C++ Reference ยท Operators
C++ bitwise operators
Reference for C++ bitwise operators &, |, ^, ~, << and >>: masks, flags, shifts, signed pitfalls, std::bitset and the C++20 <bit> helpers.
The bitwise operators work on the individual bits of integers. & (AND), | (OR) and ^ (XOR) combine two operands bit by bit, ~ flips every bit, and << and >> shift bits left or right. They are the tools for flags, masks, packed data, hashing and low-level protocols.
Operands are promoted to at least int first, so ~ on an unsigned char produces an int full of extra one bits. Shifts have several undefined cases; know them before using shifts on signed values.
unsigned flags = 0b0101; flags |= 0b0010; // set a bit: 0b0111 flags &= ~0b0001u; // clear a bit: 0b0110
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Syntax
a & b a | b a ^ b ~a a << n a >> n a &= b a |= b a ^= b a <<= n a >>= n
| Name | Description |
|---|---|
a & b |
Each result bit is 1 only if both bits are 1. |
a | b |
Each result bit is 1 if either bit is 1. |
a ^ b |
Each result bit is 1 if the bits differ. |
~a |
Flips every bit of the promoted value. |
a << n |
Shifts bits left by n, filling with zeros. |
a >> n |
Shifts bits right by n. Unsigned and non-negative values fill with zeros; negative signed values are filled with the sign bit (arithmetic shift, guaranteed since C++20). |
Returns: a prvalue of the promoted type of the (left) operand.
Errors / undefined behavior: shifting by a negative count or by an amount greater than or equal to the bit width of the promoted left operand is undefined behavior. Since C++20, left-shifting a signed value is defined modulo 2^N; before C++20, shifting a one into or past the sign bit was undefined. Overloaded << and >> on streams have nothing to do with bit shifting (Stream operators).
Usage notes
- Use unsigned types for masks and shifts:
1u << 31is fine,1 << 31shifts into the sign bit of anint, which was not portable before C++20. - Precedence trap: comparison binds tighter than
&, soflags & MASK == 0meansflags & (MASK == 0). Always write(flags & MASK) == 0. - Promotion:
~cfor auint8_tis anint; cast or mask to get the narrow result back. - Set, clear, toggle, test:
x |= bit,x &= ~bit,x ^= bit,(x & bit) != 0. std::bitset<N>: a fixed-size bit array with the same operators pluscount(),test(),to_string().- Enum flags: scoped
enum classvalues do not convert implicitly. Overload|and&for the enum, or use a plain enum with an underlying type. <bit>(C++20):std::popcount,std::countl_zero,std::bit_cast,std::rotland friends are portable replacements for compiler builtins.std::byteswapis C++23.- Bitwise
&onbool: compiles, but evaluates both sides; see Logical operators.