August 17, 2022, Learn eTutorial

1603

With the help of examples, we will learn about bitwise operators in C++ in this tutorial.

Bitwise operators in C++ operate on integer data at the individual bit level. These operations include bit testing, bit setting, and bit shifting.

For an example

`a & b;`

a | b;

To perform operations on individual bits in C++, bitwise operators are used. They are only compatible with char and int data types. There are mainly 6 bitwise operators in C++

operator | Its description |
---|---|

& | Bitwise AND |

| | Bitwise OR |

^ | Bitwise XOR |

~ | Bitwise ONE’S complement |

<< | Bitwise shift left |

>> | Bitwise shift right |

These operators are required because the computer's CPU's Arithmetic-Logic Unit (ALU) performs arithmetic operations at the bit level.

If and only if both operands are 1, the bitwise AND & operator returns 1. If not, it will return 0.

The operation of the bitwise AND operator is shown in the following table. Let a and b be two operands that only accept binary values, i.e., 1 and 0, respectively.

The above given table is referred to as the "Truth Table" for the bitwise AND operator.

Let's examine the bitwise AND operation of two integers, 15 and 36

```
#include <iostream>
using namespace std;
int main() {
// declare variables
int a = 15, b = 36;
cout << "a = " << a << endl;
cout << "b = " << b << endl;
cout << "a & b = " << (a & b) << endl;
return 0;
}
```

**Output:**

a = 15 b = 36 a & b = 4

In the preceding example, we declared two variables, a and b. Take note of the line here:

```
cout << "a & b = " << (a & b) << endl;
```

We're doing bitwise AND operation between the variables a and b here.

If at least one of the operands is 1, the bitwise OR | operator returns 1. If not, it returns 0.

The bitwise OR operator is demonstrated in the truth table below. Let a and b be two operands with only binary values, 1 or 0.

Let's examine the bitwise OR operation of two integers, 15 and 36

```
#include <iostream>
int main() {
int a = 15, b = 36;
cout << "a = " << a << endl;
cout << "b = " << b << endl;
cout << "a | b = " << (a | b) << endl;
return 0;
}
```

**Output:**

a = 15 b = 36 a | b = 47

If and only if one of the operands is 1, the bitwise XOR ^ operator returns 1. However, if both operands are zero or one, the result is zero.

The bitwise XOR operator is demonstrated in the truth table below. Let a and b be two operands with only binary values, 1 or 0.

Let's examine the bitwise XOR operation of two integers, 12 and 25

```
#include <iostream>
int main() {
int a = 12, b = 25;
cout << "a = " << a << endl;
cout << "b = " << b << endl;
cout << "a ^ b = " << (a ^ b) << endl;
return 0;
}
```

**Output:**

a = 12 b = 25 a ^ b = 21

The bitwise XOR of a = 12 and b = 25 produces the result 21.

The bitwise complement operator is a kind of a unary operator (works mainly on only one operand). It is represented by **~**, which will convert binary digits 1 to 0 and 0 to 1.

Notably, the bitwise complement of any integer N is equal to -(N + 1).

let us consider an integer 37. According to the rule, the bitwise complement of 37 should be -(37+ 1) = -38. Let's check to see if we got the right answer .

In the preceding example, the bitwise complement of **00100101 **(37) is **11011010**. When we convert this result to decimal, we will get 218.

It is significant to note that we cannot simply convert the result to decimal and obtain the desired output. This is due to the fact that the binary result **11011010** is also equivalent to -38.

To understand this, we must first compute the binary output of -38. To compute the binary of negative integers, we use 2's complement.

- -N is the 2's complement of a number N.
- 1's complement in binary arithmetic converts 0 to 1 and 1 to 0.
- And if we add 1 to the result of the 1's complement, we get the original number's 2's complement.

For Example,

The 2's complement of 38 (i.e. -38) is 11011010 in this case. In the previous section, we calculated the bitwise complement of 37, and this value is equivalent to that number.

As a result, the bitwise complement of 37 is -38.

```
#include <iostream>
int main() {
int num1 = 25;
int num2 = -160;
cout << "~(" << num1 << ") = " << (~num1) << endl;
cout << "~(" << num2 << ") = " << (~num2) << endl;
return 0;
}
```

**Output:**

~(25) = -26 ~(-160) = 159

We declared two integer variables, num1, and num2, and initialized them with values of 25 and -160, respectively, in the preceding example.

We then computed and displayed their bitwise complements using the codes **(~num1) and (~num2)**, respectively.

The bitwise complement of **25 = - (25 + 1) = -26**

i.e. **~35 = -36**

The bitwise complement of **-160 = - (-160 + 1) = - (-159) = 159**

i.e**. ~(-160) = 159**

In C++ programming, there are two shift operators:

- Right shift operator
**>>** - Left shift operator
**<<**

The right shift operator shifts all bits to the right by a specified number of bits. It is indicated by >>.

When we shift a number to the right, we discard the least significant bits and replace them with zeroes.

We have a 4-bit number, as shown in the image above. Each individual bit is shifted to the right by one bit when we perform a one-bit right shift operation on it. As a result, the right-most bit is discarded, leaving the left-most bit empty. This vacancy has been filled by a 0.

The left shift operator shifts all bits to the left by a specified number of bits. It is represented by the symbol <<.

One bit left shift is shown here

We have a 4-bit number, as shown in the image above. Each individual bit is shifted to the left by one bit when we perform a 1 bit left shift operation on it.

As a result, the left-most bit is discarded, while the right-most bit is left empty. This vacancy has been filled by a 0.

```
#include <iostream>
int main() {
// declaring two integer variables
int num = 212, i;
// Shift Right Operation
cout << "Shift Right:" << endl;
// Using for loop for shifting num right from 0 bit to 3 bits
for (i = 0; i < 4; i++) {
cout << "212 >> " << i << " = " << (212 >> i) << endl;
}
// Shift Left Operation
cout << "\nShift Left:" << endl;
// Using for loop for shifting num left from 0 bit to 3 bits
for (i = 0; i < 4; i++) {
cout << "212 << " << i << " = " << (212 << i) << endl;
}
return 0;
}
```

**Output:**

Shift Right: 212 >> 0 = 212 212 >> 1 = 106 212 >> 2 = 53 212 >> 3 = 26 Shift Left: 212 << 0 = 212 212 << 1 = 424 212 << 2 = 848