Lesson 20 of 60 – Bitwise Operators in C++
33%

Bitwise Operators in C++

Bitwise operators are used to perform operations directly on the individual bits of integral values. They are commonly used in low-level programming, flags, masks, embedded systems, and performance-sensitive code.

Note: Bitwise operators work with integral types such as int, unsigned int, char, and other integer types. They are different from logical operators such as && and ||.

1. What are Bitwise Operators?

Bitwise operators work with the binary representation of integer values. A number is represented internally using bits, where each bit can have a value of 0 or 1.

int a = 5;
int b = 3;

In binary, these values can be represented as:

5 = 0101
3 = 0011

2. Bitwise Operators in C++

Operator Name
& Bitwise AND
| Bitwise OR
^ Bitwise XOR
~ Bitwise NOT
<< Left Shift
>> Right Shift

3. Bitwise AND Operator &

The & operator performs a bitwise AND operation. A bit in the result is 1 only when both corresponding bits are 1.

int a = 5;
int b = 3;

int result = a & b;

Binary representation:

  0101
& 0011
------
  0001

Therefore, the result is 1.

4. Bitwise AND Truth Table

Bit A Bit B A & B
0 0 0
0 1 0
1 0 0
1 1 1

Only the combination 1 & 1 produces 1.

5. Bitwise OR Operator |

The | operator performs a bitwise OR operation. A result bit is 1 when at least one of the corresponding bits is 1.

int a = 5;
int b = 3;

int result = a | b;

Binary representation:

  0101
| 0011
------
  0111

The result is 7.

6. Bitwise OR Truth Table

Bit A Bit B A | B
0 0 0
0 1 1
1 0 1
1 1 1

7. Bitwise XOR Operator ^

The ^ operator performs a bitwise exclusive OR operation. A result bit is 1 when the two corresponding bits are different.

int a = 5;
int b = 3;

int result = a ^ b;

Binary representation:

  0101
^ 0011
------
  0110

The result is 6.

8. Bitwise XOR Truth Table

Bit A Bit B A ^ B
0 0 0
0 1 1
1 0 1
1 1 0

XOR produces 1 when the two bits are different.

9. Bitwise NOT Operator ~

The ~ operator is called bitwise NOT. It flips every bit: 0 becomes 1 and 1 becomes 0.

unsigned int a = 5;

unsigned int result = ~a;

The exact numeric result depends on the width of the unsigned type. For example, if a value is considered using 8 bits:

5  = 00000101
~5 = 11111010

10. Understanding Bitwise NOT

Bitwise NOT operates on every bit of the operand.

unsigned char a = 5;
unsigned char result = static_cast<unsigned char>(~a);

Using an unsigned type can make the bit pattern easier to understand when learning bitwise operations.

11. Left Shift Operator <<

The << operator shifts the bits of an integer to the left by a specified number of positions.

int number = 5;

int result = number << 1;

For a suitable non-negative value, shifting left by one position corresponds to multiplying by 2.

5  = 0101
5<<1 = 1010

The resulting value is 10 in this example.

12. Left Shift by Two Positions

int number = 5;

int result = number << 2;

The bits are shifted two positions to the left. For this positive example, the result is 20.

5       = 0101
5<<2    = 10100

13. Right Shift Operator >>

The >> operator shifts bits to the right.

unsigned int number = 20;

unsigned int result = number >> 2;

For an unsigned value, shifting right by two positions gives 5.

20      = 10100
20>>2   = 00101

14. Right Shift by One Position

unsigned int number = 16;

unsigned int result = number >> 1;

The result is 8. For an unsigned value, shifting right by one position moves every bit one position toward the right.

15. Bitwise Operators with Variables

int a = 12;
int b = 10;

int andResult = a & b;
int orResult = a | b;
int xorResult = a ^ b;

Each operation works on the individual bits of a and b.

16. Bitwise AND for Checking Bits

Bitwise AND is often used with a mask to check whether a particular bit is set.

int number = 5;

if (number & 1) {
    std::cout << "Odd";
}

For non-negative integers, checking the least significant bit with & 1 can determine whether the number is odd.

17. Checking Even and Odd Numbers

The least significant bit of a non-negative integer is 0 for an even number and 1 for an odd number.

int number = 8;

if ((number & 1) == 0) {
    std::cout << "Even";
} else {
    std::cout << "Odd";
}

18. Using Bit Masks

A bit mask is a value used to select or test particular bits.

unsigned int number = 10;
unsigned int mask = 4;

if ((number & mask) != 0) {
    std::cout << "The selected bit is set";
}

The AND operation keeps only the bits selected by the mask.

19. Setting a Bit Using OR

Bitwise OR can be used with a mask to set selected bits to 1.

unsigned int number = 8;
unsigned int mask = 2;

number = number | mask;

The bit represented by the mask is set in the resulting value.

20. Clearing a Bit Using AND and NOT

A selected bit can be cleared using a combination of AND and NOT.

unsigned int number = 15;
unsigned int mask = 4;

number = number & ~mask;

The bits selected by mask are cleared in the result.

21. Toggling a Bit Using XOR

XOR can be used to toggle selected bits. A selected bit changes from 0 to 1 or from 1 to 0.

unsigned int number = 10;
unsigned int mask = 2;

number = number ^ mask;

Only the bit selected by the mask is toggled.

22. Compound Bitwise Assignment Operators

C++ provides compound assignment versions of several bitwise operators.

Operator Example Equivalent
&= a &= b a = a & b
|= a |= b a = a | b
^= a ^= b a = a ^ b
<<= a <<= b a = a << b
>>= a >>= b a = a >> b

23. Using Bitwise AND Assignment

int number = 12;

number &= 10;

This is equivalent to:

number = number & 10;

The bitwise AND result is stored back in number.

24. Using Bitwise OR Assignment

int number = 8;

number |= 2;

This is equivalent to:

number = number | 2;

The selected bits from the right-hand operand are combined with number.

25. Using Bitwise XOR Assignment

int number = 10;

number ^= 2;

This is equivalent to:

number = number ^ 2;

The selected bits are toggled according to the XOR operation.

26. Bitwise Operators with User Input

#include <iostream>

int main() {

    unsigned int a;
    unsigned int b;

    std::cout << "Enter first number: ";
    std::cin >> a;

    std::cout << "Enter second number: ";
    std::cin >> b;

    std::cout << "AND: " << (a & b) << std::endl;
    std::cout << "OR: " << (a | b) << std::endl;
    std::cout << "XOR: " << (a ^ b) << std::endl;

    return 0;
}

27. Displaying Binary Values

C++ can display an integer in binary using std::bitset.

#include <iostream>
#include <bitset>

int main() {

    unsigned int number = 5;

    std::cout << std::bitset<8>(number);

    return 0;
}

The output is:

00000101

28. Complete Bitwise Operators Example

#include <iostream>
#include <bitset>

int main() {

    unsigned int a = 5;
    unsigned int b = 3;

    std::cout << "A: "
              << std::bitset<8>(a) << std::endl;

    std::cout << "B: "
              << std::bitset<8>(b) << std::endl;

    std::cout << "A & B: "
              << std::bitset<8>(a & b) << std::endl;

    std::cout << "A | B: "
              << std::bitset<8>(a | b) << std::endl;

    std::cout << "A ^ B: "
              << std::bitset<8>(a ^ b) << std::endl;

    std::cout << "A << 1: "
              << std::bitset<8>(a << 1) << std::endl;

    std::cout << "A >> 1: "
              << std::bitset<8>(a >> 1) << std::endl;

    return 0;
}

29. Common Bitwise Operator Mistakes

  • Confusing bitwise & with logical &&.
  • Confusing bitwise | with logical ||.
  • Confusing bitwise ^ with exponentiation. C++ does not use ^ for powers.
  • Forgetting that bitwise operators operate on bits.
  • Using bitwise operations with floating-point values.
  • Ignoring the signedness and width of integer types when interpreting bit patterns.
  • Using invalid shift counts, such as a negative count or a count that is too large for the type.
  • Assuming signed right-shift behavior is identical on every implementation.

30. Bitwise Operators – Final Summary

Operator Name Example Purpose
& Bitwise AND a & b Sets a result bit when both bits are 1
| Bitwise OR a | b Sets a result bit when at least one bit is 1
^ Bitwise XOR a ^ b Sets a result bit when the bits are different
~ Bitwise NOT ~a Flips each bit
<< Left Shift a << 1 Shifts bits to the left
>> Right Shift a >> 1 Shifts bits to the right

📌 Key Points

  • Bitwise operators work on individual bits of integral values.
  • & performs bitwise AND.
  • | performs bitwise OR.
  • ^ performs bitwise XOR.
  • ~ performs bitwise NOT.
  • << shifts bits to the left.
  • >> shifts bits to the right.
  • Bitwise AND is useful for checking or clearing selected bits.
  • Bitwise OR can be used to set selected bits.
  • Bitwise XOR can be used to toggle selected bits.
  • Bitwise operators are different from logical operators.
  • Use unsigned integer types when you need predictable reasoning about bit patterns.

🧠 Quick Quiz

Question: What is the result of the following C++ expression?

5 & 3