Open In App

Bitwise Operators in C++

Last Updated : 22 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

There are various Operators present in C++. Every Operator has a particular symbol as well as an Operation to perform. We have various categories of operators in C++.

  1. Arithmetic Operators
  2. Relational Operators
  3. Logical Operators
  4. Assignment Operators
  5. Bitwise Operators

In this article, we will learn about the Bitwise Operators in C++.

C++ Bitwise Operators

Bitwise Operators are the operators that are used to perform operations on the bit level on the integers. While performing this operation integers are considered as sequences of binary digits. In C++, we have various types of Bitwise Operators.

  1. Bitwise AND (&)
  2. Bitwise OR (|)
  3. Bitwise XOR (^)
  4. Bitwise NOT (~)
  5. Left Shift (<<)
  6. Right Shift (>>)

1. Bitwise AND (&)

Bitwise AND operation is performed between two integers, It will compare each bit on the same position and the result bit will be set(1) only and only if both corresponding bits are set(1). The symbol which is used to perform bitwise AND operation is &.

Example: we will perform a Bitwise operation between two numbers 7 and 4. In binary 7 will be represented as 111 and 4 will be 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

2. Bitwise OR (|)

If Bitwise OR operation is performed between two integers , It will compare each bit on same position and the result bit will be set(1) if any of corresponding bits are set(1). The symbol which is used to perform bitwise OR operation is |.

Example: we will perform bitwise OR operation between two numbers 7 and 4. in binary 7 will be represented as 111 and 4 will be represented as 100.

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

as we can see in above example those bits are set bits whose atleast any one corresponding bit are set. Therefore 7|4=7.

3. Bitwise XOR (^)

If Bitwise XOR operation is performed between two integers , It will compare each bit on same position and the result bit will be set(1) if any of corresponding bits differ i.e. one of them should be 1 and other should be zero. The symbol which is used to perform bitwise XOR operation is ^.

Example: we will perform bitwise XOR operation between two numbers 7 and 4. in binary 7 will be represented as 111 and 4 will be represented as 100.

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

as we can see in above example those bits are set bits whose corresponding bits are different . Therefore 7^4=3.

4.Bitwise NOT (~)

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

Example: We will perform bitwise NOT Operation on number 4. The number 4 is represented as 100 in binary.

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

As we can see in result the bits whose initial value was 1 are 0 in result and vice-versa. Therefore Bitwise NOT of number 4 will be 3.

5.Left Shift (<<)

This operator shifts the bits of Integer to left side by specific number (As mentioned) . This left shift operation is equivalent to multiplying the integer by 2 power number of positions shifted. The symbol which is used to represent 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 x << 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. Now , we will move all the bits two positions to left and we will 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 2 which is 4 . 5*4 = 20 shows the above mentioned statement.

6.Right Shift (>>)

This operator shifts the bits of Integer to right side by specific number (As mentioned) . This right shift operation is equivalent to dividing the integer by 2 power number of positions shifted. The symbol which is used to represent Left 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 x >> 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. Now , we will move all the bits two positions to right and we will 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 2 which is 4 . 16*4 = 4 shows the above mentioned statement.

C++ Program Bitwise Operators

Below is the implementation of the topic:

C++
// C++ Program to demonstrate
// Bitwise Operator
#include <iostream>

using namespace std;

// Main function
int main()
{
    int a = 5; //  101
    int b = 3; //  011

    // Bitwise AND
    int bitwise_and = a & b;

    // Bitwise OR
    int bitwise_or = a | b;

    // Bitwise XOR
    int bitwise_xor = a ^ b;

    // Bitwise NOT
    int bitwise_not = ~a;

    // Bitwise Left Shift
    int left_shift = a << 2;

    // Bitwise Right Shift
    int right_shift = a >> 1;

      // Printing the Results of
    // Bitwise Operators
    cout << "AND: " << bitwise_and << endl;
    cout << "OR: " << bitwise_or << endl;
    cout << "XOR: " << bitwise_xor << endl;
    cout << "NOT a: " << bitwise_not << endl;
    cout << "Left Shift: " << left_shift << endl;
    cout << "Right Shift: " << right_shift << endl;

    return 0;
}

Output:

AND: 1
OR: 7
XOR: 6
NOT a: -6
Left Shift: 20
Right Shift: 2

Conclusion

In Conclusion, Bitwise Operators are used to perform operations on binary (bit) level. We have various kind of Bitwise Operators as AND, OR, XOR, NOT, left shift, and right shift operators in C++. By this operations manipulation of individual bits can be done very precisely Which is essential in low level data handling.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads