Bitwise operations

What are bitwise operations?

Bitwise operations are operations that operate directly on the bits of variables and numbers. Note that they do not work on floating point variables and numbers. In C/C++, there are 6 bitwise operators.

Before explaining what they do, here is the list:

OperatorDescription
&Bitwise AND
|Bitwise OR
^Bitwise XOR
~Bitwise complement
>>Right shift
<<Left shift

Note that a bit which value is 1 is said to be set and reset (Or cleared) otherwise.

The operator & (AND)

The operator & takes two integer operands of the same size and looks at each of their bits that are at the same position. If both bits at the same position are equal to 1, the bit at that position inside the returned value will be equal to 1, otherwise 0.

For example, it looks at the first bit of the left operand and at the first bit of the right operand. If both bits equal 1, then, in the value returned, the first bit will be set (equal to 1) and unset otherwise.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream> int main() { unsigned char n1 = 243; // 243 in binary: 11110011b. unsigned char n2 = 240; // 240 in binary: 11110000b. unsigned char n3 = n1 & n2; std::cout << (int)n3 << std::endl; // 240 is written to the console. /* 11110011b & 11110000b = 11110000b */ return 0; }

The operator | (OR)

The operator | also takes two integer operands of the same size and looks at each of their bits that are at the same position. When at least one of the 2 bits at the same position equals 1, it returns 1 at that bit position, otherwise 0.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream> int main() { unsigned char n1 = 235; // 235 in binary: 11101011b. unsigned char n2 = 176; // 176 in binary: 10110000b. unsigned char n3 = n1 | n2; std::cout << (int)n3 << std::endl; // 251 is written into the console. // 251 in binary: 11111011b. /* 11101011b | 10110000b = 11111011b */ return 0; }

The operator ^ (XOR)

The operator ^ also takes two integer operands of the same size and looks at each of their bits that are at the same position. When one, and only one, of the 2 bits at the same position equals 1, it returns 1 at that bit position, otherwise 0.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream> int main() { unsigned char n1 = 235; // 235 in binary: 11101011b. unsigned char n2 = 176; // 176 in binary: 10110000b. unsigned char n3 = n1 ^ n2; std::cout << (int)n3 << std::endl; // 91 is written in the console. // 91 in binary: 01011011b. /* 11101011b ^ 10110000b = 01011011b */ return 0; }

The operator ~ (Complement)

The operator ~ takes one integer operand, as right operand, and inverts all of its bit. Ones become zeros and zeros become ones.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream> int main() { unsigned char n = 235; // 235 in binary: 11101011b. n = ~n; std::cout << (int)n << std::endl; // 20 is written in the console. // 20 in binary: 00010100b. /* ~ 11101011b = 00010100b */ return 0; }

The operator >> (Right shift)

The operator >> moves the bits of the left operand to the right (Toward the least significant bits) by the number of bit position represented by the right operand. The bits overflowing to the right are discarded and zeros are added to the left.

Note that the value of the right operand must be positive.

Let us say n is the right operand. Then, n zeros are added at the left of the left operand and n binary digits are removed from the right of it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream> int main() { unsigned char n = 235; // 235 in binary: 11101011b. n = n >> 4; std::cout << (int)n << std::endl; // 14 is written in the console. // 14 in binary: 00001110b. /* 11101011b >> 4 = 00001110b */ return 0; }

The operator << (Left shift)

The operator << does the same as the >> operator, except that it moves the bits of the left operand to the left (Toward the most significant bits).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream> int main() { unsigned char n = 235; // 235 in binary: 11101011b. n = n << 4; std::cout << (int)n << std::endl; // 176 is written in the console. // 176 in binary: 10110000b. /* 11101011b << 4 = 10110000b */ return 0; }