Home C++ Bitwise operators in C++ tutorial with code examples

Bitwise operators in C++ tutorial with code examples

Bitwise operators in C++ perform operations on individual bits of integer data types, allowing manipulation at the binary level.

Bitwise operations are commonly used in low-level programming, digital signal processing, and embedded systems.

List of Bitwise Operators in C++

Operator Name Description
& Bitwise AND Sets each bit to 1 if both bits are 1
` ` Bitwise OR
^ Bitwise XOR Sets each bit to 1 if the bits are different
~ Bitwise NOT Inverts all the bits
<< Left Shift Shifts bits to the left and fills 0s on the right
>> Right Shift Shifts bits to the right and fills 0s (for unsigned)

1. Bitwise AND (&)

The bitwise AND operator compares each bit of its operands. If both bits are 1, the result is 1; otherwise, it's 0.

#include <iostream>
using namespace std;

int main() {
    int a = 5;      // Binary: 0101
    int b = 3;      // Binary: 0011
    int result = a & b; // Binary: 0001

    cout << "a & b = " << result << endl; // Output: 1
    return 0;
}

Explanation:

  • 5 in binary is 0101, and 3 is 0011.
  • The result of 0101 & 0011 is 0001, which is 1 in decimal.

Output:

a & b = 1

2. Bitwise OR (|)

The bitwise OR operator sets each bit to 1 if either of the bits is 1.

#include <iostream>
using namespace std;

int main() {
    int a = 5;      // Binary: 0101
    int b = 3;      // Binary: 0011
    int result = a | b; // Binary: 0111

    cout << "a | b = " << result << endl; // Output: 7
    return 0;
}

Explanation:

  • 5 in binary is 0101, and 3 is 0011.
  • The result of 0101 | 0011 is 0111, which is 7 in decimal.

Output:

a | b = 7

3. Bitwise XOR (^)

The bitwise XOR operator sets each bit to 1 if the bits are different.

#include <iostream>
using namespace std;

int main() {
    int a = 5;      // Binary: 0101
    int b = 3;      // Binary: 0011
    int result = a ^ b; // Binary: 0110

    cout << "a ^ b = " << result << endl; // Output: 6
    return 0;
}

Explanation:

  • 5 in binary is 0101, and 3 is 0011.
  • The result of 0101 ^ 0011 is 0110, which is 6 in decimal.

Output:

a ^ b = 6

4. Bitwise NOT (~)

The bitwise NOT operator inverts all the bits of the operand.

#include <iostream>
using namespace std;

int main() {
    int a = 5;      // Binary: 0101
    int result = ~a; // Binary (two's complement): 1010

    cout << "~a = " << result << endl; // Output: -6
    return 0;
}

Explanation:

  • 5 in binary is 0101. The bitwise NOT operation flips all bits to 1010, which represents -6 in two's complement notation.

Output:

~a = -6

5. Left Shift (<<)

The left shift operator shifts bits to the left by the specified number of positions, filling in 0s from the right.

#include <iostream>
using namespace std;

int main() {
    int a = 5;      // Binary: 00000101
    int result = a << 1; // Binary: 00001010

    cout << "a << 1 = " << result << endl; // Output: 10
    return 0;
}

Explanation:

  • Shifting 5 (binary 00000101) to the left by 1 bit results in 00001010, which is 10 in decimal.

Output:

a << 1 = 10

6. Right Shift (>>)

The right shift operator shifts bits to the right by the specified number of positions, filling in 0s from the left for unsigned integers.

#include <iostream>
using namespace std;

int main() {
    int a = 5;      // Binary: 00000101
    int result = a >> 1; // Binary: 00000010

    cout << "a >> 1 = " << result << endl; // Output: 2
    return 0;
}

Explanation:

  • Shifting 5 (binary 00000101) to the right by 1 bit results in 00000010, which is 2 in decimal.

Output:

a >> 1 = 2

7. Using Bitwise Operators for Setting, Clearing, and Toggling Bits

Bitwise operators are useful for manipulating individual bits of a number, such as setting, clearing, and toggling specific bits.

Setting a Bit

To set a bit at position n in a number num, use the bitwise OR operator with 1 shifted to position n.

#include <iostream>
using namespace std;

int main() {
    int num = 5;      // Binary: 0101
    int pos = 1;      // Position to set (0-indexed)
    num |= (1 << pos); // Set bit at position 1

    cout << "After setting bit 1, num = " << num << endl; // Output: 7
    return 0;
}

Explanation:

  • Setting the bit at position 1 in 0101 results in 0111, which is 7 in decimal.

Output:

After setting bit 1, num = 7

Clearing a Bit

To clear a bit at position n, use the bitwise AND operator with 1 shifted to n, then bitwise NOT.

#include <iostream>
using namespace std;

int main() {
    int num = 5;      // Binary: 0101
    int pos = 2;      // Position to clear (0-indexed)
    num &= ~(1 << pos); // Clear bit at position 2

    cout << "After clearing bit 2, num = " << num << endl; // Output: 1
    return 0;
}

Explanation:

  • Clearing the bit at position 2 in 0101 results in 0001, which is 1 in decimal.

Output:

After clearing bit 2, num = 1

Toggling a Bit

To toggle a bit at position n, use the XOR operator with 1 shifted to n.

#include <iostream>
using namespace std;

int main() {
    int num = 5;      // Binary: 0101
    int pos = 0;      // Position to toggle (0-indexed)
    num ^= (1 << pos); // Toggle bit at position 0

    cout << "After toggling bit 0, num = " << num << endl; // Output: 4
    return 0;
}

Explanation:

  • Toggling the bit at position 0 in 0101 results in 0100, which is 4 in decimal.

Output:

After toggling bit 0, num = 4

8. Checking if a Specific Bit is Set

To check if a specific bit is set, use the bitwise AND operator with 1 shifted to the desired position. If the result is non-zero, the bit is set.

#include <iostream>
using namespace std;

int main() {
    int num = 5;      // Binary: 0101
    int pos = 2;      // Position to check (0-indexed)

    bool isSet = num & (1 << pos);
    cout << "Is bit at position " << pos << " set? " << (isSet ? "Yes" : "No") << endl;
    return 0;
}

Explanation:

  • Checking the bit at position 2 in 0101 results in true, meaning the bit is set.

Output:

Is bit at position 2 set? Yes

9. Summary Table of

Bitwise Operators in C++

Operator Name Example Description
& Bitwise AND a & b Sets each bit to 1 if both bits are 1
` ` Bitwise OR `a
^ Bitwise XOR a ^ b Sets each bit to 1 if bits are different
~ Bitwise NOT ~a Inverts all the bits
<< Left Shift a << 1 Shifts bits to the left and fills 0s on the right
>> Right Shift a >> 1 Shifts bits to the right and fills 0s (for unsigned)
` =, &=, ^=, <<=, >>=` Compound Assignment `a

Complete Example: Using Bitwise Operators

This example demonstrates various bitwise operations in C++, including setting, clearing, and toggling bits.

#include <iostream>
using namespace std;

int main() {
    int num = 8;  // Binary: 1000
    int pos = 1;  // Position to modify (0-indexed)

    cout << "Original number: " << num << endl;
    cout << "Setting bit at position " << pos << ": ";
    num |= (1 << pos); // Set bit
    cout << num << endl;

    cout << "Clearing bit at position " << pos << ": ";
    num &= ~(1 << pos); // Clear bit
    cout << num << endl;

    cout << "Toggling bit at position " << pos << ": ";
    num ^= (1 << pos); // Toggle bit
    cout << num << endl;

    pos = 3;
    bool isSet = num & (1 << pos);
    cout << "Is bit at position " << pos << " set? " << (isSet ? "Yes" : "No") << endl;

    return 0;
}

Output:

Original number: 8
Setting bit at position 1: 10
Clearing bit at position 1: 8
Toggling bit at position 1: 10
Is bit at position 3 set? Yes

Bitwise operators in C++ offer powerful ways to manipulate data at the binary level, enabling efficient operations for tasks that require bit manipulation.

You may also like