Skip to content
Tutorials Center
My learning

Topics

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.

C++
unsigned flags = 0b0101;
flags |= 0b0010;      // set a bit:    0b0111
flags &= ~0b0001u;    // clear a bit:  0b0110

Try it

C++ Flags and masks

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.

C++ Shifts and the bit library

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

C++
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 << 31 is fine, 1 << 31 shifts into the sign bit of an int, which was not portable before C++20.
  • Precedence trap: comparison binds tighter than &, so flags & MASK == 0 means flags & (MASK == 0). Always write (flags & MASK) == 0.
  • Promotion: ~c for a uint8_t is an int; 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 plus count(), test(), to_string().
  • Enum flags: scoped enum class values 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::rotl and friends are portable replacements for compiler builtins. std::byteswap is C++23.
  • Bitwise & on bool: compiles, but evaluates both sides; see Logical operators.