100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
C++

Bitwise Operators in C++

Understand C++ bitwise operators &, |, ^, ~, <<, and >> that manipulate the binary representation of integers directly.

OperatorsIntermediate7 min readJul 7, 2026
Analogies

1. Intro

Bitwise operators operate directly on the individual bits of integer values, rather than on the numbers as a whole. C++ provides six bitwise operators: & (AND), | (OR), ^ (XOR), ~ (NOT/complement), << (left shift), and >> (right shift). They're used for low-level tasks like flags, masks, and performance-sensitive bit manipulation.

🏏

Cricket analogy: A DRS system encoding multiple review flags (out, not-out, umpire's-call) into a single byte using bit flags relies on the same six bitwise operators — &, |, ^, ~, <<, >> — C++ uses for masks and performance-sensitive manipulation.

2. Syntax

cpp
a & b     // bitwise AND
a | b     // bitwise OR
a ^ b     // bitwise XOR
~a        // bitwise NOT (complement)
a << n    // left shift by n bits
a >> n    // right shift by n bits

3. Explanation

& sets a result bit to 1 only if both corresponding bits are 1. | sets a result bit to 1 if at least one corresponding bit is 1. ^ (XOR) sets a result bit to 1 if the corresponding bits differ. ~ flips every bit (0 becomes 1, 1 becomes 0). << shifts bits left, filling with zeros on the right — each left shift by 1 effectively multiplies by 2 (for non-negative values, ignoring overflow). >> shifts bits right; for unsigned integers it fills with zeros, but for signed integers the fill behavior is implementation-defined (typically sign-extends). These operators only work on integer types, not float/double.

🏏

Cricket analogy: Checking if BOTH the third umpire AND on-field umpire flagged 'out' uses AND-like logic (& sets a bit only if both inputs are 1), while flagging if EITHER flagged a no-ball mirrors |; these bitwise operators only work on whole-number flag registers, never on float run-rate values.

Do not confuse bitwise &/| with logical &&/||. & and | operate bit-by-bit on the full integer and do not short-circuit, while && and || operate on truthiness and do short-circuit. Using & where you meant && (e.g. if (a & b)) is a subtle bug that still compiles because int converts implicitly to bool.

4. Example

cpp
#include <iostream>
using namespace std;

int main() {
    unsigned int a = 12; // 1100 in binary
    unsigned int b = 10; // 1010 in binary

    cout << "a & b = " << (a & b) << endl;
    cout << "a | b = " << (a | b) << endl;
    cout << "a ^ b = " << (a ^ b) << endl;
    cout << "a << 1 = " << (a << 1) << endl;
    cout << "a >> 1 = " << (a >> 1) << endl;
    return 0;
}

5. Output

text
a & b = 8
a | b = 14
a ^ b = 6
a << 1 = 24
a >> 1 = 6

6. Key Takeaways

  • Bitwise operators (& | ^ ~ << >>) act on the binary representation of integers, not on floating-point types.
  • << by n roughly multiplies by 2^n; >> by n roughly divides by 2^n, for non-negative integers.
  • Bitwise &/| are distinct from logical &&/|| — bitwise operators don't short-circuit and work bit-by-bit.
  • ~ flips every bit of a value, effectively computing its one's complement.

Practice what you learned

Was this page helpful?

Topics covered

#CStudyNotes#Programming#BitwiseOperatorsInC#Bitwise#Operators#Syntax#Explanation#StudyNotes#SkillVeris#ExamPrep