|
| 1 | +# About |
| 2 | + |
| 3 | +## Bitwise operations |
| 4 | + |
| 5 | +Bitwise operations allow us to manipulate individual digits within binary numbers. |
| 6 | +These operations are fundamental in computing, providing an efficient way to perform low-level tasks, such as controlling specific bits in memory, optimizing mathematical calculations, encryption, communications, and encoding/decoding data. |
| 7 | + |
| 8 | +### Understanding 32-bit Integers in Elm |
| 9 | + |
| 10 | +In Elm, integers are stored as 32-bit signed integers using [two's complement](https://en.wikipedia.org/wiki/Two%27s_complement) representation, meaning they use 32 binary digits (_bits_) to store each integer value. |
| 11 | +This bit limit affects the range of integers that Elm can represent directly: |
| 12 | + |
| 13 | +Positive Range: 0 to 2<sup>31</sup> - 1 (or 0 to 2,147,483,647) |
| 14 | +Negative Range: -2<sup>31</sup> to -1 (or -2,147,483,648 to -1). |
| 15 | + |
| 16 | +For example, the integer `5` is represented in binary as `00000000000000000000000000000101`, although usually we ignore the leading zeros and just say `101`. |
| 17 | + |
| 18 | +### Bitwise |
| 19 | + |
| 20 | +Elm provides several bitwise operators in its [Bitwise module](https://package.elm-lang.org/packages/elm/core/latest/Bitwise) |
| 21 | + |
| 22 | +## Basic operations |
| 23 | + |
| 24 | +Modifying individual bits of a number is called _masking_. |
| 25 | +A _mask_ is a number where specific bits have been set in a particular way to manipulate another number using bitwise operators such as `and`, `or`, and `xor`. |
| 26 | + |
| 27 | +### and |
| 28 | + |
| 29 | +`and` combines two numbers by keeping only the bits that are `1` in both. |
| 30 | +This is useful for checking to see if an individual bit is set. |
| 31 | +For example, to check if the 4th bit of a number is set to `1`, `and` it with a mask of `01000` (`8` in decimal) and see if the result is non-zero: |
| 32 | + |
| 33 | +```elm |
| 34 | +Bitwise.and 13 8 --> 8 |
| 35 | +-- 13 = 01101 |
| 36 | +-- 8 = 01000 |
| 37 | +-- and = 01000 = 8 |
| 38 | +``` |
| 39 | + |
| 40 | +### or |
| 41 | + |
| 42 | +`or` combines two numbers by setting each bit to `1` if it is `1` in either or both numbers. |
| 43 | +This is useful for setting a specific bit to `1`. |
| 44 | +For example, to set the 2nd bit in `10101`, `or` it with the mask `00010`: |
| 45 | + |
| 46 | +```elm |
| 47 | +Bitwise.or 21 2 --> 23 |
| 48 | +-- 21 = 10101 |
| 49 | +-- 2 = 00010 |
| 50 | +-- or = 10111 = 23 |
| 51 | +``` |
| 52 | + |
| 53 | +### Exclusive-or (xor) |
| 54 | + |
| 55 | +`xor` combines two numbers by setting each bit to `1` if it is `1` in one number but `0` in the other. |
| 56 | +This is useful for flipping a bit to its opposite value: |
| 57 | + |
| 58 | +```elm |
| 59 | +Bitwise.xor 20 5 --> 17 |
| 60 | +-- 20 = 10100 |
| 61 | +-- 5 = 00101 |
| 62 | +-- xor = 10001 = 17 |
| 63 | +``` |
| 64 | + |
| 65 | +### Complement |
| 66 | + |
| 67 | +`complement` inverts each bit of a number (`0` becomes `1`, `1` becomes `0`). |
| 68 | + |
| 69 | +Note that this will result in positive numbers becoming negative, and negative numbers becoming positive. |
| 70 | +This is because negative numbers in binary are represented with `1` in the left-most position. |
| 71 | + |
| 72 | +```elm |
| 73 | +Bitwise.complement 21 --> -22 |
| 74 | +-- 21 = 00000000000000000000000000010101 |
| 75 | +-- complement = 11111111111111111111111111101010 = -22 |
| 76 | +``` |
| 77 | + |
| 78 | +### Bit shifting |
| 79 | + |
| 80 | +The following operators move bits left or right by a specified number of positions, effectively multiplying or dividing by powers of 2. |
| 81 | + |
| 82 | +`shiftLeftBy` moves bits to the left, filling in with `0` from the right-hand side. |
| 83 | +For example, to shift `21` left by 3 places: |
| 84 | + |
| 85 | +```elm |
| 86 | +Bitwise.shiftLeftBy 3 21 --> 168 |
| 87 | +-- 21 = 10101 |
| 88 | +-- shiftLeftBy 3 = 10101000 = 168 |
| 89 | +``` |
| 90 | + |
| 91 | +This is the same as saying `21 * 2^3 = 21 * 2 * 2 * 2 = 168` |
| 92 | + |
| 93 | +`shiftRightBy`: Moves bits to the right: |
| 94 | + |
| 95 | +```elm |
| 96 | +Bitwise.shiftRightBy 2 21 --> 5 |
| 97 | +-- 21 = 10101 |
| 98 | +-- shiftRightBy 2 = 00101 = 5 |
| 99 | +``` |
| 100 | + |
| 101 | +Shifting to the right by 2 places is the same as integer division by 4. |
| 102 | + |
| 103 | +Note that this function duplicates whatever value is in the leftmost bit. |
| 104 | +So, negative numbers will stay negative: |
| 105 | + |
| 106 | +```elm |
| 107 | +Bitwise.shiftRightBy 3 -21 --> -3 |
| 108 | +-- -21 = 111...101011 |
| 109 | +-- shiftRightBy 3 = 111...11101 = -3 |
| 110 | +``` |
| 111 | + |
| 112 | +If you want to shift right and fill in with zeros, use `shiftRightZfBy`: |
| 113 | + |
| 114 | +```elm |
| 115 | +Bitwise.shiftRightZfBy 3 -21 --> 536870909 |
| 116 | +-- -21 = 111...101011 |
| 117 | +-- shiftRightZfBy 3 = 00111...11101 = 536870909 |
| 118 | +``` |
0 commit comments