Contents
Introduction
Bitwise Operators always scared developers (with no offence) but once developers understood the power of it, developers love them.
Bitwise operator are very powerful operators if understood correctly. Its operators on ints and uints at the binary level. This means they look directly at the binary digits or bits of an integer.
Types of Bitwise Operator
Bitwise AND (a & b)
Bitwise AND is like logical AND in functioning, the bit- wise AND is applied to each bit of the both the operand, it returns one if all the bits are ‘1’ else it always returns ‘0’.
Truth table of bitwise AND:
Decimal Value | Binary Value | |||||||
5 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 1 |
6 | 0 | 0 | 0 | 0 | 0 | 1 | 1 | 0 |
0 | 0 | 0 | 0 | 0 | 1 | 0 | 0 |
So (5&6) output will be 4.
Bitwise OR (a | b)
Bitwise OR is like logical OR in functioning, the bitwise OR is applied to each bit of the both the operand, it returns ‘1’ if any the bits are ‘1’, it returns ‘0’ only if all the bits are ‘0’.
Truth table of bitwise OR:
Decimal Value | Binary Value | |||||||
5 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 1 |
6 | 0 | 0 | 0 | 0 | 0 | 1 | 1 | 0 |
0 | 0 | 0 | 0 | 0 | 1 | 1 | 1 |
So (5|6) output will be 7.
Bitwise XOR (a ^ b)
Bitwise XOR is like logical XOR in functioning, the bit- wise XOR is applied to each bit of the both the operand, it simply adds the bits and discard the remainder i.e. it returns ‘1’ if both the bits are different and ‘0’ if both the bits are same.
Truth table of bitwise XOR:
Decimal Value | Binary Value | |||||||
5 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 1 |
6 | 0 | 0 | 0 | 0 | 0 | 1 | 1 | 0 |
0 | 0 | 0 | 0 | 0 | 0 | 1 | 1 |
So (5 ^ 6) output will be 3.
Bitwise NOT (~ a)
Bitwise NOT is a unary operator that flips the bits of the number i.e., if the ith bit is 0, it will change it to 1 and vice versa. Bitwise NOT is nothing but simply the one’s complement of a number. Let’s take an example.
Truth table of bitwise Not:
Binary Value | |||||||
0 | 0 | 0 | 0 | 0 | 1 | 0 | 1 |
1 | 1 | 1 | 1 | 1 | 0 | 1 | 0 |
Left shift (a << b)
The left operand specifies the value to be shifted. The right operand specifies the number of positions that the bits in the value are to be shifted. When shifting left, the most-significant bit is lost, and a 00 bit is inserted on the other end.
if (5 << 1 ) output will be 10.
Binary Value | |||||||
5 | 0 | 0 | 0 | 0 | 1 | 0 | 1 |
10 | 0 | 0 | 0 | 1 | 0 | 1 | 0 |
if (5 << 2 ) output will be 20.
Binary Value | |||||||
5 | 0 | 0 | 0 | 0 | 1 | 0 | 1 |
20 | 0 | 0 | 1 | 0 | 1 | 0 | 0 |
Right shift (a>>b)
In right shift operator every bit in the operand is simply moved a given number of bit positions towards right. The right most bits are drop and new bits are inserted in from left. On the bases of which bit to be inserted from the left, right shift is divided into two type, logical right shift and arithmetic right shift.
Logical right shift
When shifting right with a logical right shift, the least-significant bit is lost and a 00 is inserted on the other end.
If (5 >>> 1) output will be 2
Binary Value | |||||||
0 | 0 | 0 | 0 | 0 | 1 | 0 | 1 |
0 | 0 | 0 | 0 | 0 | 0 | 1 | 0 |
For positive numbers, a single logical right shift divides a number by 2, throwing out any remainders.
Arithmetic Right Shifts
When shifting right with an arithmetic right shift, the least-significant bit is lost, and the most-significant bit is copied.
Languages handle arithmetic and logical right shifting in different ways.
Application of Bitwise operator:
Below are the real-world use cases of the following bitwise operators
1. Communication over ports/sockets
2. Compression, Encryption
3. Graphics
4. Computer Drivers