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
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 bits3. 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
#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
a & b = 8
a | b = 14
a ^ b = 6
a << 1 = 24
a >> 1 = 66. 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
1. What is `12 & 10` in decimal (12 = 1100, 10 = 1010 in binary)?
2. What is `5 << 1` equal to?
3. What is the key difference between `&` and `&&` in C++?
4. What does the `~` operator do to an integer?
5. What is `6 ^ 3` (6 = 0110, 3 = 0011 in binary)?
Was this page helpful?
You May Also Like
Arithmetic Operators in C++
Learn how C++ arithmetic operators (+, -, *, /, %) work, including integer division truncation and modulo rules, with runnable examples.
Data Types in C++
Learn the fundamental data types in C++ — int, float, double, char, bool, and void — with sizes, ranges, and examples.
Type Modifiers in C++
Learn what type modifiers are in C++ — signed, unsigned, short, and long — and how they change size and range.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics