Bitwise Operators in C++

Last Updated : 16 Jan, 2026

In C++, bitwise operators perform operations directly on the binary representation of integers, making them useful for low-level and system-level programming. C++ provides 6 bitwise operators which are as follows:

1. Bitwise AND (&)

Bitwise AND operation compare each bit at the same position in the integer and the resultant bit will be set (1) only and only if both corresponding bits are set (1), otherwise it will be unset (0). The symbol which is used to perform bitwise AND operation is &.

Example: Let's perform a bitwise AND operation between two numbers- 7 and 4. In binary, 7 is represented as 111 and 4 is represented as 100.

1 1 1
& 1 0 0
------
1 0 0

As we can see in the above example, only those bits are set bits whose corresponding bits (both) are set. Therefore 7 & 4 = 4

Code Example:

C++
#include <iostream>
using namespace std;

int main() {
    cout<< (7&4) <<endl;
    return 0;
}

Output
4

2. Bitwise OR (|)

The Bitwise OR operation compares each bit at the same position, and the result bit will be set (1) if any of the corresponding bits are set (1). The symbol used to perform the bitwise OR operation is |.

Example: We will perform a bitwise OR operation between two numbers, 7 and 4.

1 1 1
| 1 0 0
------
1 1 1

As we can see in the above example, the bits are set if at least one of the corresponding bits is set. Therefore, 7 | 4 = 7.

Code Example:

C++
#include <iostream>
using namespace std;

int main() {
    cout << (7|4) <<endl;
    return 0;
}

Output
7

3. Bitwise XOR (^)

The Bitwise XOR operation compares each bit at the same position, and the result bit will be set (1) if the corresponding bits differ, i.e., one should be 1 and the other should be 0. The symbol used to perform the bitwise XOR operation is ^.

Example: We will perform a bitwise XOR operation between two numbers, 7 and 4.

1 1 1
^ 1 0 0
------
0 1 1

As we can see in the above example, the bits are set where the corresponding bits differ. Therefore, 7 ^ 4 = 3.

Code Example:

C++
#include <iostream>
using namespace std;

int main() {

    cout << (7^4) <<endl;
    return 0;
}

Output
3

4.Bitwise NOT (~)

The Bitwise NOT operation is performed on a single number. It changes the current bit to its complement, i.e., if the current bit is 0, then in the result it will be 1, and if the current bit is 1, it will become 0. It is denoted by the symbol ~.

Example: We will perform the Bitwise NOT operation on the number 4.

~ 1 0 0
------
0 1 1

As we can see in the result, the bits whose initial value was 1 become 0, and the bits whose initial value was 0 become 1.

Code Example:

C++
#include <iostream>
using namespace std;

int main() {

    cout << (~4) <<endl;
    return 0;
}

Output
-5

5. Left Shift (<<)

The leftshift operator shifts the bits of an integer to the left by a specific number of positions (as mentioned). This left shift operation is equivalent to multiplying the integer by 2 raised to the power of the number of positions shifted. The symbol used to represent the left shift operator is <<.

Example: Consider we have an integer 5, and we will left-shift its bits by 2 positions. The operation will be represented as

5 << 2

The number 5 is represented as 101 in binary. We will add some zeros at the beginning to left-shift the bits. Therefore, it will be represented as 00000101.

0 0 0 0 0 1 0 1 << 2
------------
0 0 0 1 0 1 0 0

Now, we will move all the bits two positions to the left and fill the empty positions with 0. Therefore, it will become 00010100, which is 20. As mentioned earlier, left-shifting the number by two bits means multiplying it by 2 raised to the power of 2, which is 4. So, 5 * 4 = 20.

Code Example:

C++
#include <iostream>
using namespace std;

int main() {

    cout << (5<<2) <<endl;;
    return 0;
}

Output
20

6. Right Shift (>>)

The right shift operator shifts the bits of an integer to the right by a specific number of positions. This right shift operation is equivalent to dividing the integer by 2 raised to the power of the number of positions shifted. The symbol used to represent the right shift operator is >>.

Example: Consider we have an integer 16, and we will right-shift its bits by 2 positions. The operation will be represented as:

16 >> 2;

The number 16 is represented as 10000 in binary. We will add some zeros at the beginning to right-shift the bits. Therefore, it will be represented as 00010000.

0 0 0 1 0 0 0 0 >> 2
------------
0 0 0 0 0 1 0 0

Now, we will move all the bits two positions to the right and fill the empty positions with 0. Therefore, it will become 00000100, which is 4. As mentioned earlier, right-shifting the number by two bits means dividing it by 2 raised to the power of 2, which is 4. So, 16 / 4 = 4.

Code Example:

C++
#include <iostream>
using namespace std;

int main() {

    cout << (16>>2) <<endl;
    return 0;
}
Try it on GfG Practice
redirect icon

Output
4
Comment