6 Bitwise Operators You Should Know
The basic bitwise operators
A bitwise operator is an operator used to perform bitwise operations on bit patterns or binary numerals that involve the manipulation of individual bits.
There are some basic bitwise operators that we need to know
- Bitwise AND (&)
- Bitwise OR (|)
- Bitwise NOT (~)
- Bitwise XOR (^)
- Left Shift (<<)
- Right Shift (>>)
Now let’s look at each of them in-depth
1. Bitwise AND (&)
The bitwise AND is a binary operator, which means it require two operands to manipulate them and return a result.
The bitwise AND operator (&) returns a 1 in each bit position for which the corresponding bits of both operands are 1 s, and 0 if either of the bits are 0.
As we can see in the truth table, AND (&) only returns 1 if both operands are 1, and it returns 0 if either one of the operands are 0.
We can clearly see from the code above that, each bit of both operands are performing AND(&) operation to produce resulting bit.
2. Bitwise OR (|)
The bitwise OR is a binary operator, which means it require two operands to manipulate them and return a result.
In bitwise OR operator, if both of the bits are 0 then result of that bit is 0, otherwise, the result is 1 .
As we can see in the truth table, OR (|) returns 1 if any of the operands are 1, and it returns 0 if both of the operands are 0.
On the above code, each bit of both operands are performing OR(|) operation to produce resulting bit.
3. Bitwise NOT (~)
The bitwise NOT is an unary operator, which means it require only one operand to produce a result.
In bitwise NOT operator, if a bit is 0 then result is 1, and if a bit is 1 then result is 0. Basically the bits are inverted.
As we can see in the truth table, NOT (~) inverts the bits and return 1 if operand is 0 and returns 0 if operand is 1. It changes each bit to its opposite.
If you are confused how we got decimal “-13” from binary “11110011”, remember that the binary is in signed 2’s complement form.
So in order to covert it into binary :
- Leftmost bit is 1, which indicates it is negative ( 1=Negative, 0=Positive)
- As it is negative, we’ll covert it to 1’s complement form by subtracting 1. 11110011 -1 = 11110010
- Now we get it’s positive unsigned representation, by flipping all bits.
!(11110010) = 00001101 - Now we get the number as 13, and if we add the negative sign to it, we get -13.
This is how we convert signed 2’s complement to binary.
4. Bitwise XOR (^)
The bitwise XOR is a binary operator, which means it require two operands to manipulate them and return a result.
In bitwise XOR operator, if both of the bits are same then result of that bit is 0, or else if both bits are different then the result is 1 .
As we can see from the truth table, if both bits are same, like both 1’s or both 0’s then the result is 0, and if both bits are different then the result is 1.
On the above code, each bit of both operands are performing XOR(^) operation to produce resulting bit.
The next 2 operations are a little different from the above ones. The concept is pretty easy, the whole idea is moving bits to left or right. Let’s look at both bitwise left shift and right shift operators and their working.
5. Bitwise Left Shift (<<)
Two operands are needed for this, first operand is the number on which we perform the left shift, and the second operand decides the number of places to shift.
“4 << 2” means 4 is left shifted by 2 positions. It essentially moves the bits to the left and adds zeroes on the right. In this case the bits of 4 is moved to left by 2 places and 2 zeroes are added on the right. The bits which overflow are ignored. The last bit in the direction of shift, in this case the leftmost bit is lost every time we do a left shift.
As we can see from the above code, by left shifting by 2, the binary of 4 is shifted 2 places to the left and zeroes are added in the right.
Essentially, except the case of losing bits with value 1 from the left, it doubles the number each time we shift left.
6. Bitwise Right Shift (>>)
Two operands are needed for this, first operand is the number on which we perform the right shift, and the second operand decides the number of places to shift.
“4 >> 2” means 4 is right shifted by 2 positions. It essentially moves the bits to the right and adds zeroes on the left. In this case the bits of 4 is moved to right by 2 places and 2 zeroes are added on the left. The bits which overflow are ignored. The last bit in the direction of shift, in this case the rightmost bit is lost every time we do a right shift.
As we can see from the above code, by right shifting by 2, the binary of 4 is shifted 2 places to the right and zeroes are added in the left.
Essentially, Shifting right is dividing by two and rounding down.
I hope this will give you a general idea about bit manipulation and bitwise operators. Bit Manipulation is a technique used in a variety of problems to get the solution in an optimized way. It is fast and efficient.
If you found this useful, be sure to like and share. Do you like bit manipulation? Let me know in the comments!