C++ Bitwise operators

Bitwise operators are used to performing operations on individual bits of binary numbers. These operators are used in various fields such as cryptography, image processing, and network programming.

There are six bitwise operators available in C language

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

Here is a table that summarizes the bitwise operators in C language:

Operator
Description
Example
&
Bitwise AND
a & b
|
Bitwise OR
a | b
^
Bitwise XOR (exclusive OR)
a ^ b
~
Bitwise NOT (one's complement)
~a
<<
Bitwise left shift
a << b
>>
Bitwise right shift
a >> b

Bitwise AND (&)

If and only if both of the operands have the respective bits set to 1, the bitwise AND operator produces a result with all of its bits set to 1. If not, it returns 0.

Example:

int a = 60; // Binary value: 00111100
int b = 13; // Binary value: 00001101
int result = a & b; // Binary value: 00001100

In this example, the bitwise AND operator is used to compare each corresponding bit of the binary values of a and b. The resulting binary value 00001100 is equal to the decimal value 12.

Bitwise OR (|)

The bitwise OR operator returns a value which has all the bits set to 1 if either one or both of the operands have the corresponding bits set to 1. Otherwise, it returns 0.

Example:

int a = 60; // Binary value: 00111100
int b = 13; // Binary value: 00001101
int result = a | b; // Binary value: 00111101

In this example, the bitwise OR operator is used to compare each corresponding bit of the binary values of a and b. The resulting binary value 00111101 is equal to decimal value 61.

Bitwise XOR (^)

The bitwise XOR operator returns a value that has all the bits set to 1 if the corresponding bits of the operands are different. Otherwise, it returns 0.

Example:

int a = 60; // Binary value: 00111100
int b = 13; // Binary value: 00001101
int result = a ^ b; // Binary value: 00110001

In this example, the bitwise XOR operator is used to compare each corresponding bit of the binary values of a and b. The resulting binary value 00110001 is equal to the decimal value 49.

Bitwise NOT (~)

The bitwise NOT operator returns the one’s complement of the operand. It changes each bit of the operand from 0 to 1, and vice versa.

Example:

int a = 60; // Binary value: 00111100
int result = ~a; // Binary value: 11000011

In this example, the bitwise NOT operator is used to changing each bit of the binary value of a from 0 to 1, and vice versa. The resulting binary value 11000011 is equal to the decimal value -61.

Bitwiese left shift (<<)

The left shift operator shifts the bits of the left operand to the left by the number of positions specified by the right operand. The vacated bits on the right are filled with zeros.

Example:

int a = 60; // Binary value: 00111100
int result = a << 2; // Binary value: 11110000

In this example, the left shift operator is used to shift the binary value of a two positions to the left. The vacated bits on the right are filled with zeros. The resulting binary value 11110000 is equal to decimal value 240.

Bitwise right shift (>>)

The right shift operator shifts the bits of the left operand to the right by the number of positions specified by the right operand. The vacated bits on the

Example:

#include <stdio.h>

int main() {
    int a = 12;
    int b = a >> 2; // right shift a by 2 bits
    printf("a = %d\n", a); // output: a = 12
    printf("b = %d\n", b); // output: b = 3
    return 0;
}

In this example, we have initialized an integer variable a with a value of 12. We then use the right shift operator (>>) to shift the bits of a two positions to the right. The resulting value is stored in a new integer variable b.