Skip to content Skip to sidebar Skip to footer

Bitwise Operator in C++

Bitwise Operator is related to binary numbers, so before starting with this type of operator, let’s know something about binary numbers. Binary numbers are a combination of 0’s and 1’s, which makes the low-level language, or in simpler terms, a language that a computer can understand but not us humans. Since we know about binary numbers, let us start learning what Bitwise Operators are.

What are Bitwise Operators?

Bitwise Operators operate on the binary conversion of an integer directly or when we store it in a variable. With its help, we can test, shift, or set an actual bit. We can only use this operator with int and char data types

But how do we do this? Well, with the help of the different types of operators it offers.

Types Of Bitwise Operators

We have about 6 types of bitwise operators:

  1. Bitwise AND Operator ( & )
  2. Bitwise OR Operator ( | )
  3. Bitwise XOR Operator ( ^ )
  4. Bitwise COMPLEMENT Operator ( ~ )
  5. Bitwise SHIFT LEFT Operator ( << )
  6. Bitwise SHIFT RIGHT Operator ( >> )

Let us know more about them in detail.

Bitwise AND Operator ( & )

This operator is just like the logical And operator, but the only difference between them is that the bitwise AND operator works with the integers on their binary bit level. 

When we use this operator, the result comes out 1 only and only if both of the two operands contain 1. If both the operands are 0 or either one is 0, then the result is 0.

Let us understand it with the help of its truth table.

Assume X and Y to be the two operands that only hold binary values, 0 and 1;

XYX & Y
000
010
100
111

With the help of this truth table, let us understand what the computer does when we ask questions like a & b, assuming the values a and b hold are 10 and 16.

a=10
b=16

//Binary numbers
10= 1010
16= 10000

//performing bitwise AND operation
a & b

//when we have 1 bit less we add an extra 0 to the left of thenumber
     01010
  &  10000
----------
     00000      // it is the binary of number 0

Bitwise OR operator ( | )

” This or that “is the simplest explanation of this operator. When we use this operator, the result comes out as 1 one of the two operands contains 1or both the operands contain 1. If both the operands are 0, then the result is 0.

Let us understand with the help of the truth table.

Assume X and Y to be the two operands that only hold binary values, 0 and 1;

XYX | Y
000
011
101
111

With the help of this truth table, let us understand what the computer does when we ask questions like a | b, assuming the values a and b hold are 10 and 16.

a=10
b=16

//Binary numbers
10= 1010
16= 10000

//performing bitwise OR operation
a | b

//when we have 1 bit less, we add an extra 0 to the left of the number
    01010
|   10000
-----------
    11010      //is the binary form of the number 26

Bitwise XOR Operator ( ^ )

When we use this operator, the result comes out 1 only and only if one of the two operands contains 1. If both the operands are 0 or both are 1, then the result comes out to be 0.

Let us understand with the help of the truth table.

Assume X and Y to be the two operands that only hold binary values, 0 and 1;

XYX ^ Y
000
011
101
110

With the help of this truth table, let us understand what the computer does when we ask questions like a ^ b, assuming the values a and b hold are 10 and 16.

a=10
b=16

//Binary numbers
10= 1010
16= 10000

//performing bitwise XOR operation
a ^ b

//when we have 1 bit less, we add an extra 0 to the left of the number
    01010
  ^ 10000
---------
    11010     //is the binary form of the number 26

Binary COMPLEMENT Operator ( ~ )

When we use this operator, it transforms the 0’s to 1’s and 1’s to 0’s of a binary number, which means it works on a single operand (unary operator). Hence, this operator is also known as one’s complement operator.

 Let us understand what the computer does when we ask questions like a, assuming the value a holds is 10.

a=10

//Binary numbers
10= 001010

//performing bitwise COMPLEMENT operation
a = ~a

001010= 110101   // it is the binary form of number 53

n= -(n+1)
10= -11

// Binary number of 11 is 001011
//1's complement

   110100
+       1       // this gets 2's complement
---------
   110101       // binary of number 53 or -11

Now you might be thinking about why we calculated the 2’s complement, and even after applying 2’s complement to the -(10+1) number, you might have seen that we got the same result as earlier. Why so?

It is because it is not the correct answer. As per the rule of the bitwise complement operator, the answer should be -(N+1)( i.e., -(10+1)=-11 )unlike the answer(53) we calculated using the one’s complement method. 

And here comes the reason why we used 2’s complement. It is because the result of any number N is -N. For example, if we have a number 53, then after applying 2’s complement, the result will be -53

So, to get the answer -11, we calculated the 2’s complement of the number 11.

Bitwise SHIFT LEFT Operator (<<)

We use this operator when we need to shift the bits of the number to the left, and we have vacant space; we fill it with the help of 0.

Let us understand with the help of an example.


a=10

//Binary number
10= 001010

//Performing bitwise SHIFT LEFT operator (<<)
10 << 2

00 - 001010 + 00       //we shit 2 bits(00) to the left and add 2 bits(00) to the right.

//result
101000   // Binary of number 40

Bitwise SHIFT Right Operator (>>)

We use this operator when we need to shift the bits of the number to the right and have vacant space; we fill it with the help of 0.

Let us understand with the help of an example.

a=10

//Binary number
10= 001010

//Performing bitwise SHIFT LEFT operator (<<)
10 >> 2

00 + 001010 - 10       //we shit 2 bits(10) to the right and add 2 bits(00) to the left.

//result
000010   // Binary of number 2

Example of all the Bitwise Operators Combined

//Example of all the Bitwise Operators Combined
#include <iostream>
using namespace std;

int main()
{
    unsigned int a = 10; // 10 = 00 1010
    unsigned int b = 16; // 16 = 01 0000
    int c = 0;

    c = a & b; // Bitwise AND Operator
    cout << "a & b = " << c << endl;

    c = a | b; // Bitwise OR Operator
    cout << "a | b = " << c << endl;

    c = a ^ b; // Bitwise XOR Operator
    cout << "a ^ b = " << c << endl;

    c = ~a; // Bitwise Complement Operator
    cout << "~a = " << c << endl;

    c = a << 2; // Bitwise SHIFT LEFT Operator
    cout << "a << 2 = " << c << endl;
    
    c = a >> 2; // Bitwise SHIFT RIGHT Operator
    cout << "a >> 2 = " << c << endl;

    return 0;
}

Output

a & b = 0
a | b = 26
a ^ b = 26
~a = -11
a << 2 = 40
a >> 2 = 2

CONCLUSION

In this blog, we’ve learned about another category of Operators known as Bitwise Operators and have taken another step in our journey of learning  C++ programming. We got to know what they are, their types, and how they make calculating the binary factor among two operands super easy—hoping to learn many more related topics shortly.

As you all know, GeekonPeak has just begun and is looking for some loyal supporters so we, the new-gen geeky friends, can grow and create more comprehensive and easy-to-grasp courses for you (for free). Follow us on FacebookInstagram & Twitter as well. Also, don’t miss our newsletter subscription. We provide exclusive blogs, tips, tricks, and advice to program efficiently and market smartly. We also promote some of our partners with excellent experiences to share with you. Until next time 🙂 

Leave a comment

Sign Up to Our Newsletter

Be the first to know the latest updates