Bitwise operations work directly on the binary representation of integers.
Operator
Symbol
Example
Meaning
Bitwise left shift
<<
x << k
Shifts all bits of x to the left by k positions
Bitwise right shift
>>
x >> k
Shifts all bits of x to the right by k positions
Bitwise AND
&
x & y
Applies AND to each pair of corresponding bits
Bitwise OR
|
x | y
Applies OR to each pair of corresponding bits
Bitwise exclusive OR
^
x ^ y
Applies XOR to each pair of corresponding bits
Bitwise Left Shift
When we shift the binary number
(an−1an−2…a1a0)2
to the left by k positions, we append k zeros to the right:
(an−1an−2…a1a000…0)2
So shifting left by one position is equivalent to multiplying by 2, and shifting left by k positions is equivalent to multiplying by 2k.
The output is:
44
Bitwise Right Shift
When we shift the binary number
(an−1an−2…a1a0)2
to the right by k positions, the last k bits are removed:
(an−1an−2…ak)2
For non-negative integers, shifting right by one position is equivalent to integer division by 2, and shifting right by k positions is equivalent to integer division by 2k.
The output is:
2
Bitwise AND
The bitwise AND of two numbers (a)2 and (b)2 is a number (c)2, where
ci=ai&bi
This means we apply AND to each pair of corresponding bits.
The result of x & y is 1 only if both bits are 1. Otherwise, it is 0.
AND table
x
y
Result
0
0
0
0
1
0
1
0
0
1
1
1
The output is:
2
Bitwise OR
The bitwise OR of two numbers (a)2 and (b)2 is a number (c)2, where
ci=ai∣bi
This means we apply OR to each pair of corresponding bits.
The result of x | y is 1 if at least one of the bits is 1. Otherwise, it is 0.
OR table
x
y
Result
0
0
0
0
1
1
1
0
1
1
1
1
The output is:
13
Bitwise Exclusive OR (XOR)
The bitwise XOR of two numbers (a)2 and (b)2 is a number (c)2, where
ci=ai⊕bi
This means we apply XOR to each pair of corresponding bits.
The result of x ^ y is 1 if exactly one of the bits is 1. If both bits are the same, the result is 0.
XOR table
x
y
Result
0
0
0
0
1
1
1
0
1
1
1
0
The output is:
10
Example Problems
Try to solve these problems on your own before looking at the solutions.
Example 1
Given two integers x(0≤x≤29) and y(0≤y≤29), where x=y, output the value
2x+2y
Example 2
Given two integers x(0≤x≤229) and k(0≤k≤29), output the number obtained from x after inverting the k-th bit.
Example 3
Given two integers x(0≤x≤229) and k(0≤k≤29), output the number obtained from x after setting the k-th bit to 0.
Example 4
Given two integers x(0≤x≤229) and k(0≤k≤29), output the value of the k-th bit of the number x.
Example 5
Given two integers x(0≤x≤229) and k(0≤k≤29), set the last k bits of the number x to 0 and output the result.
Example 6
Given two integers x(0≤x≤109) and y(0≤y≤109), compute their product without using the operations *, /, or %.
Solutions to the Examples
Example 1
To get 2x, we can use 1 << x. Similarly, 1 << y gives 2y.
Since x=y, these two numbers have different single bits set, so we can combine them with bitwise OR.
Example 2
To invert the k-th bit, we use XOR with a mask that has only the k-th bit equal to 1.
Example 3
To set the k-th bit to 0, we can first check whether it is 1. If it is, we flip it.
A shorter version is:
Example 4
To get the value of the k-th bit, we shift it to the last position and take the last bit.
Example 5
Solution with a loop
We check the last k bits one by one and clear them if needed.
Solution without a loop
We shift the number right by k bits, which removes the last k bits, and then shift it back left by k bits.
Example 6
We can use the binary representation of y.
If the i-th bit of y is 1, then we add x⋅2i, which is the same as x << i.
int x = 11; // x = 1011 in binarycout <<(x << 2)<< "\n"; // 101100 in binary
int x = 11; // x = 1011 in binarycout <<(x >> 2)<< "\n"; // 10 in binary
int x = 11; // x = 1011int y = 6; // y = 0110cout <<(x & y)<< "\n"; // 0010 in binary
int x = 9; // x = 1001int y = 4; // y = 0100cout <<(x | y)<< "\n"; // 1101 in binary
int x = 12; // x = 1100int y = 6; // y = 0110cout <<(x ^ y)<< "\n"; // 1010 in binary
cout <<((1<< x) |(1<< y))<< "\n";
cout <<(x ^(1<< k))<< "\n";
if(x &(1<< k)){ x ^=(1<< k);}cout << x << "\n";
cout <<(x &(~(1<< k)))<< "\n";
cout <<((x >> k) & 1)<< "\n";
for(int i = 0; i < k; i++){ if(x &(1<< i)){ x ^=(1<< i);}}cout << x << "\n";
cout <<((x >> k)<< k)<< "\n";
long long res = 0;for(int i = 0; i < 30; i++){ if(y &(1<< i)){ res +=(1LL * x << i);}}cout << res << "\n";