Write a C++ Program that illustrates the bitwise operators.

**Bitwise operators in C++** are operators that operate on individual bits of integers. They include AND (&), OR (|), XOR (^), NOT (~), left shift (<<), and right shift (>>). These operators allow manipulation and combination of bits, providing greater control over data storage and processing.

Basic bitwise operators in C++ are essential for manipulating individual bits of integers. They include:

**1. AND Operator (&):**

The AND operator compares the corresponding bits of two integers. It sets the resulting bit to 1 only if both input bits are 1; otherwise, it sets the bit to 0.

**Example**:

int a = 12, b = 10; // Binary: 1100 & 1010 int result = a & b; // result = 8 (Binary: 1000)

In this example, the AND operator compares each pair of bits (1 & 1, 1 & 0, 0 & 1, 0 & 0) and sets the resulting bits accordingly (i.e. 1000).

**2. OR Operator (|):**

The OR operator compares the corresponding bits of two integers. It sets the resulting bit to 1 if either of the input bits is 1. If both input bits are 0, it sets the bit to 0.

**Example**:

int a = 12, b = 10; // Binary: 1100 & 1010 int result = a | b; // result = 14 (Binary: 1110)

Here, the OR operator compares each pair of bits (1 | 1, 1 | 0, 0 | 1, 0 | 0) and sets the resulting bits accordingly (i.e. 1110).

**3. XOR Operator (^):**

The XOR operator compares the corresponding bits of two integers. It sets the resulting bit to 1 only if the input bits are different (one is 0, and the other is 1). If the input bits are the same (both 0 or both 1), it sets the bit to 0.

**Example**:

int a = 12, b = 10; // Binary: 1100 & 1010 int result = a ^ b; // result = 6 (Binary: 0110)

In this case, the XOR operator compares each pair of bits (1 ^ 1, 1 ^ 0, 0 ^ 1, 0 ^ 0) and sets the resulting bits accordingly.

**4. NOT Operator (~):**

The NOT operator is a unary operator that flips all the bits of an integer. It converts each 0 to 1 and each 1 to 0.

**Example**:

int a = 12; // Binary: 1100 int result = ~a; // result = -13 (assuming 2's complement representation)

Bitwise shift operators in C++ allow you to shift the bits of an integer left or right by a specified number of positions.

**1. Left Shift Operator (<<):**

The left shift operator (<<) shifts the bits of an integer to the left. Each shift moves the bits towards the most significant end, effectively multiplying the integer by 2 raised to the power of the shift amount.
**Example:**

int a = 5; int result = a << 2; // result = 20

In this example, the bits of the integer a are shifted two positions to the left, resulting in the integer 20.

**2. Right Shift Operator (>>):**

The right shift operator (>>) shifts the bits of an integer to the right. Each shift moves the bits towards the least significant end, effectively dividing the integer by 2 raised to the power of the shift amount.

**Example:**

int a = 20; int result = a >> 2; // result = 5

Here, the bits of the integer a are shifted two positions to the right, resulting in the integer 5.

Here is the source code of the C++ program which illustrates the bitwise operators. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.

`/*`

`* C++ Program to Illustrate Bitwise Operators`

`*/`

`#include <iostream>`

using namespace std;

int main() {

int a = 7; // a = 111

int b = 5; // b = 101

cout << "Bitwise Operators\n";

cout << "a & b = " << (a&b) << "\n";

cout << "a | b = " << (a|b) << "\n";

cout << "a ^ b = " << (a^b) << "\n";

cout << "~a = " << (~a) << "\n";

cout << "~b = " << (~b) << "\n";

cout << "a >> b = " << (a>>b) << "\n";

cout << "a << b = " << (a<<b) << "\n";

`}`

1. Two integer variables, **a** and **b**, are declared and initialized with values 7 and 5, respectively.

2. The program outputs a message “Bitwise Operators” using the cout statement.

3. The program performs various bitwise operations and displays the results using cout statements:

- (
**a & b):**Performs bitwise AND operation between a and b and prints the result. **(a | b):**Performs bitwise OR operation between a and b and prints the result.**(a ^ b):**Performs bitwise XOR operation between a and b and prints the result.**(~a):**Performs bitwise NOT operation on a and prints the result.**(~b):**Performs bitwise NOT operation on b and prints the result.**(a >> b):**Performs right shift operation on a by b positions and prints the result.**(a << b):**Performs left shift operation on a by b positions and prints the result.

4. The program execution completes, and the main function returns 0, indicating successful termination.

**Time Complexity: O(1)**

The time complexity of the above program is constant O(1) since it performs a fixed number of operations regardless of the input size.

**Space Complexity: O(1)**

The space complexity is also constant as it uses a fixed amount of memory for the variables.

$ gcc test.cpp $ a.out Bitwise Operators a & b = 5 a | b = 7 a ^ b = 2 ~a = -8 ~b = -6 a >> b = 0 a << b = 224

To practice programs on every topic in C++, please visit “Programming Examples in C++”, “Data Structures in C++” and “Algorithms in C++”.

**Related Posts:**

- Apply for C++ Internship
- Check C++ Books
- Apply for Computer Science Internship
- Check Computer Science Books
- Apply for Information Technology Internship