|
| 1 | +# Bit Manipulation |
| 2 | + |
| 3 | +Is a technique used in programming to perform operations at the bit level, which can often lead to more efficient and faster algorithms. |
| 4 | + |
| 5 | +#### When is bit manipulation useful? |
| 6 | + |
| 7 | +Bit manipulation allows us to work directly with the binary representation of numbers, making certain operations more efficient. Common tasks such as setting, clearing, toggling and checking bits can be performed quickly using bitwise operation. |
| 8 | + |
| 9 | +For example, one of the most common space optimization techniques involves using an unsigned 32-bit integer to represent a set of boolean values, where each bit in the integer corresponds to a different boolean value. This allow us to store and manipulate up to 32 states without using a boolean array or hash set. |
| 10 | + |
| 11 | +## Bitwise operators |
| 12 | + |
| 13 | +### NOT (`~`) |
| 14 | + |
| 15 | +Inverts a single bit: |
| 16 | + |
| 17 | +- `~0 = 1` |
| 18 | +- `~1 = 0` |
| 19 | + |
| 20 | +| a | ~a | |
| 21 | +| --- | --- | |
| 22 | +| 0 | 1 | |
| 23 | +| 1 | 0 | |
| 24 | + |
| 25 | +--- |
| 26 | + |
| 27 | +### AND (`&`) |
| 28 | + |
| 29 | +Returns `1` **only if both bits are 1**, otherwise `0`. |
| 30 | + |
| 31 | +| a | b | a & b | |
| 32 | +| --- | --- | ----- | |
| 33 | +| 0 | 0 | 0 | |
| 34 | +| 0 | 1 | 0 | |
| 35 | +| 1 | 0 | 0 | |
| 36 | +| 1 | 1 | 1 | |
| 37 | + |
| 38 | +--- |
| 39 | + |
| 40 | +### OR (`|`) |
| 41 | + |
| 42 | +Returns `1` **if at least one bit is 1**, otherwise `0`. |
| 43 | + |
| 44 | +| a | b | a | b | |
| 45 | +| --- | --- | ---------- | |
| 46 | +| 0 | 0 | 0 | |
| 47 | +| 0 | 1 | 1 | |
| 48 | +| 1 | 0 | 1 | |
| 49 | +| 1 | 1 | 1 | |
| 50 | + |
| 51 | +--- |
| 52 | + |
| 53 | +### XOR (`^`) |
| 54 | + |
| 55 | +Returns `1` **only if the bits are different**, otherwise `0`. |
| 56 | + |
| 57 | +| a | b | a ^ b | |
| 58 | +| --- | --- | ----- | |
| 59 | +| 0 | 0 | 0 | |
| 60 | +| 0 | 1 | 1 | |
| 61 | +| 1 | 0 | 1 | |
| 62 | +| 1 | 1 | 0 | |
| 63 | + |
| 64 | +Some useful characteristics of the XOR operator are: |
| 65 | + |
| 66 | +- a ^ 0 = a |
| 67 | +- a ^ a = 0 |
| 68 | + |
| 69 | +--- |
| 70 | + |
| 71 | +In addition, it's also important to understand the fundamental shift operators. |
| 72 | + |
| 73 | +Shift operators move the bits of a number **left or right** by a specified number of positions. Zeros are filled in from the opposite end. |
| 74 | + |
| 75 | +We’ll use small values of `a` and shift amounts to keep it simple. |
| 76 | + |
| 77 | +--- |
| 78 | + |
| 79 | +### Left Shift (`<<`) |
| 80 | + |
| 81 | +Shifts all bits of `a` **to the left** by `n` positions, adding 0s on the right. |
| 82 | +Each left shift is equivalent to multiplying by `2ⁿ`. |
| 83 | + |
| 84 | +#### Example: `a << 1` |
| 85 | + |
| 86 | +| a (decimal) | a (binary) | a << 1 (binary) | Result (decimal) | |
| 87 | +| ----------- | ---------- | --------------- | ---------------- | |
| 88 | +| 1 | `0001` | `0010` | 2 | |
| 89 | +| 2 | `0010` | `0100` | 4 | |
| 90 | +| 3 | `0011` | `0110` | 6 | |
| 91 | +| 4 | `0100` | `1000` | 8 | |
| 92 | + |
| 93 | +--- |
| 94 | + |
| 95 | +### Right Shift (`>>`) |
| 96 | + |
| 97 | +Shifts all bits of `a` **to the right** by `n` positions, discarding bits on the right. |
| 98 | +Each right shift is equivalent to integer division by `2ⁿ`. |
| 99 | + |
| 100 | +Assumes unsigned or logical shift (no sign extension). |
| 101 | + |
| 102 | +#### Example: `a >> 1` |
| 103 | + |
| 104 | +| a (decimal) | a (binary) | a >> 1 (binary) | Result (decimal) | |
| 105 | +| ----------- | ---------- | --------------- | ---------------- | |
| 106 | +| 1 | `0001` | `0000` | 0 | |
| 107 | +| 2 | `0010` | `0001` | 1 | |
| 108 | +| 3 | `0011` | `0001` | 1 | |
| 109 | +| 4 | `0100` | `0010` | 2 | |
| 110 | + |
| 111 | +--- |
| 112 | + |
| 113 | +--- |
| 114 | + |
| 115 | +### Common Bit Manipulation Techniques |
| 116 | + |
| 117 | +These are foundational bitwise operations frequently used in systems programming, embedded development, competitive programming, and algorithm design: |
| 118 | + |
| 119 | +- **Set the i<sup>th</sup> bit to 1** |
| 120 | + Force the bit at position `i` to `1` (regardless of its current value). |
| 121 | + `x |= (1 << i)` |
| 122 | + |
| 123 | +- **Clear the i<sup>th</sup> bit (set to 0)** |
| 124 | + Force the bit at position `i` to `0`, leaving other bits unchanged. |
| 125 | + `x &= ~(1 << i)` |
| 126 | + |
| 127 | +- **Toggle the i<sup>th</sup> bit** |
| 128 | + Flip the bit at position `i`: `1` becomes `0`, and `0` becomes `1`. |
| 129 | + `x ^= (1 << i)` |
| 130 | + |
| 131 | +- **Check if the i<sup>th</sup> bit is set** |
| 132 | + Returns true if the bit at position `i` is `1`, false if `0`. |
| 133 | + `(x & (1 << i)) != 0` |
| 134 | + |
| 135 | +- **Check if a number is even or odd** |
| 136 | + The least significant bit (LSB) determines parity: |
| 137 | + |
| 138 | + - `x & 1 == 0` → **even** |
| 139 | + - `x & 1 == 1` → **odd** |
| 140 | + |
| 141 | +- **Check if a number is a power of 2** |
| 142 | + A power of two has exactly one bit set. |
| 143 | + `x > 0 && (x & (x - 1)) == 0` |
| 144 | + |
| 145 | +--- |
| 146 | + |
| 147 | +## Real-World Application: Networking and Data Integrity |
| 148 | + |
| 149 | +Bit manipulation is essential in low-level systems, especially in networking protocols and data transmission: |
| 150 | + |
| 151 | +- **IP Addressing and Subnetting** |
| 152 | + Bitwise AND is used to compute network addresses: |
| 153 | + `IP & SubnetMask` determines if two hosts are on the same network. |
| 154 | + |
| 155 | +- **Error Detection & Correction** |
| 156 | + Protocols like checksums, CRC, and parity bits use bitwise operations to detect and fix transmission errors with minimal overhead. |
| 157 | + |
| 158 | +- **Protocol Flags and Compression** |
| 159 | + Flags in protocol headers are packed as bits to save space and processing time. Bit masking efficiently sets, clears, and checks those flags. |
0 commit comments