Bitwise operators are operators that operate on integers and units at the binary level. This means that they are looking directly at binary digits or bits of an integer. It all sounds scary, but in truth, bitwise operators are quite easy to use and also very useful. In this article, I will introduce you to Bitwise operators in C ++ programming language.

## Bitwise operators in C ++

Let’s start with the Bitwise operators you should know in the C ++ programming language.

Bitwise Operators | Description |
---|---|

& | Bitwise And |

| | Bitwise OR |

^ | Bitwise XOR |

<< | Bitwise Left Shift |

>> | Bitwise Right Shift |

**Also, Read – The Fundamentals of C++ Programming Language.**

#### Bitwise OR:

The first Bitwise operator in C ++ that you should know about is Bitwise OR. Let’s see how to use this operator:

int a = 5; // 0101b (0x05) int b = 12; // 1100b (0x0C) int c = a | b; // 1101b (0x0D) std::cout << "a = " << a << ", b = " << b << ", c = " << c << std::endl;

a = 5, b = 12, c = 13

A bitwise OR works at a bitwise level and uses the following Boolean truth table:

true OR true = true true OR false = true false OR false = false

When the binary value of a (0101) and the binary value of b (1100) are combined by OR, we get the binary value of 1101.

#### Bitwise XOR (exclusive OR):

The second Bitwise operator in C ++ that you should know about is Bitwise XOR which stands for exclusive OR. Let’s see how to use this operator:

int a = 5; // 0101b (0x05) int b = 9; // 1001b (0x09) int c = a ^ b; // 1100b (0x0C) std::cout << "a = " << a << ", b = " << b << ", c = " << c << std::endl;

a = 5, b = 9, c = 12

A bitwise (or exclusive) XOR works at the bitwise level and uses the following Boolean truth table:

true OR true = false true OR false = true false OR false = false

Note that with an XOR operation true OR true = false while with the operations true AND / OR true = true, hence the exclusive nature of the XOR operation.

#### Bitwise AND:

The next Bitwise operator in C ++ that you should know about is the AND operator. Let’s see how to use it:

int a = 6; // 0110b (0x06) int b = 10; // 1010b (0x0A) int c = a & b; // 0010b (0x02) std::cout << "a = " << a << ", b = " << b << ", c = " << c << std::endl;

a = 6, b = 10, c = 2

A bitwise AND works at the bitwise level and uses the following Boolean truth table:

TRUE AND TRUE = TRUE TRUE AND FALSE = FALSE FALSE AND FALSE = FALSE

When the binary value for a (0110) and the binary value for b (1010) are combined, we get the binary value 0010.

#### Â Bitwise << – Left Shift:

The next bit operator that you should know in the C ++ programming language is the left shift operator. Let’s see how to use this operator:

int a = 1; // 0001b int b = a << 1; // 0010b std::cout << "a = " << a << ", b = " << b << std::endl;

a = 1, b = 2

Bit shifting left shifts the bits of the value on the left (a) by the number specified on the right (1), essentially filling the least significant bits with 0’s, thus shifting the value from 5 (binary 0000 0101) to the left 4 times (for example 5 << 4) will give the value of 80 (binary 0101 0000).

You may notice that shifting a value to the left 1 time is also equivalent to multiplying the value by 2.

#### Bitwise >> – Right Shift:

The next binary operator that you should know in the C ++ programming language is the right shift operator. Let’s see how to use this operator:

int a = 2; // 0010b int b = a >> 1; // 0001b std::cout << "a = " << a << ", b = " << b << std::endl;

a = 2, b = 1

Right bit shift shifts the bits of the left value (a) by the number specified to the right (1); It should be noted that while the operation of a right shift is standard, what happens to the bits of a right shift on a signed negative number is implementation-defined and therefore cannot be guaranteed to be portable.

I hope you liked this article on the Bitwise operators that you need to know in the C++ programming language. Feel free to ask your valuable questions in the comments section below.