What is Bitwise Manipulation?
Bitwise manipulation is when we perform a logical operation against each individual bit of a binary number. The logical connectives a.k.a. the bitwise operators AND
, OR
, and XOR
can be used as masks that can affect the specific bits.
What is the point of Bitwise manipulation?
Human readability is certainly not the point, but speed and efficiency are. Bitwise operations are primitive actions that can be executed directly on the CPU meaning that they stake up less temporary/persistent storage and require less pre-processing overhead to compute. You can think of them like shortcuts.
AND
The AND logical operation can be used to turn off certain bits of a binary number, because:
1 AND 0 is 0
0 AND 0 is 0
Consider you have an input of 1101 1011 and a bitwise mask of 1111 1000. Examine the function table below:
Input | 1 | 1 | 0 | 1 | 1 | 0 | 1 | 1 |
Mask | 1 | 1 | 1 | 1 | 1 | 0 | 0 | 0 |
Result | 1 | 1 | 0 | 1 | 1 | 0 | 0 | 0 |
Notice that the mask only applies to the last three digits. The third to last digit is unchanged only because it was already 0.
OR
The AND logical operation can be used to turn off certain bits of a binary number, because:
1 AND 0 is 0
0 AND 0 is 0
Consider you have an input of 1001 1011 and a bitwise mask of 1110 0000. Examine the function table below:
Input | 1 | 0 | 0 | 1 | 1 | 0 | 1 | 1 |
Mask | 1 | 1 | 1 | 0 | 0 | 0 | 0 | 0 |
Result | 1 | 1 | 1 | 1 | 1 | 0 | 1 | 1 |
Notice that only the first three digits changed (except the first digit which was already a 1). The OR switch changed all the 0 bits to 1’s.
XOR
The XOR (Exlusive OR) logical function can be used to reverse certain bits of a binary number, because:
0 XOR 1 is 1
1 XOR 1 is 0
Consider you have an input of 1011 1001 and a bitwise mask of 0000 1111. Examine the function table below:
Input | 1 | 0 | 1 | 1 | 1 | 0 | 0 | 1 |
Mask | 0 | 0 | 0 | 0 | 1 | 1 | 1 | 1 |
Result | 1 | 0 | 1 | 1 | 0 | 1 | 1 | 0 |
Notice that all four of the bits aligned with the mask are now the opposite of what they once were without exceptions. The bits have essentially been “flipped”.
Logical Shift Left
Performing a logical shift one bit to the left on a binary number means:
- Moving all the bits of the number one place to the left
- Discarding the most significant bit
- Putting a 0 into the empty place on the right
Interestingly this is equivalent to multiply our binary number by 2. Notice that performing a logical shift three bits to the left on a binary number is the same as multiplying the number by 23 = 8
.
Example:
If we start with the decimal number 14
, three logical shifts to the left of its binary form results in the decimal number 112
:
128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 | |
1410 | 0 | 0 | 0 | 0 | 1 | 1 | 1 | 0 |
2810 | 0 | 0 | 0 | 1 | 1 | 1 | 0 | 0 |
5610 | 0 | 0 | 1 | 1 | 1 | 0 | 0 | 0 |
11210 | 0 | 1 | 1 | 1 | 0 | 0 | 0 | 0 |
Logical Shift Right
Performing a logical shift one bit to the right on a binary number means:
- Moving all the bits of the number one place to the right
- Discarding the least significant bit
- Putting a 0 into the empty place on the left
As you might have guessed, this is equivalent to dividing our number by 2. Notice that performing a logical shift two bits to the right on a binary number is the same as dividing the number by 22 = 4
.
Example:
If we start with the decimal number 112
, two logical shifts to the right of its binary form results in the decimal number 28
:
128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 | |
11210 | 0 | 1 | 1 | 1 | 0 | 0 | 0 | 0 |
5610 | 0 | 0 | 1 | 1 | 1 | 0 | 0 | 0 |
2810 | 0 | 0 | 0 | 1 | 1 | 1 | 0 | 0 |