CKTIS / C++
Bitwise AND operator (&)
The binary & operator computes bitwise AND: a result bit is 1 only when both corresponding operand bits are 1. Use it to select bits with a mask; it is not a replacement for logical &&.
Version: C++23; repository compiler flag, production runtime not attested
Syntax
lhs & rhsThe built-in operator lhs & rhs compares corresponding bits: only a pair of 1 bits produces 1; every other pair produces 0. For example, 0b1101u & 0b0110u produces 0b0100u, or 4. The token bitand is an alternative spelling of &.
Operands must have integral or unscoped enumeration type (unscoped enum). The usual arithmetic conversions, including integral promotions, apply before the operation; the common type is also the result type. The result therefore need not have the smaller operand's type or type bool. Prefer matching unsigned types in masking examples. The operator groups left-to-right, but that does not establish operand evaluation order.
A mask preserves only those value bits where the mask itself has 1. The condition (value & mask) != 0u tests whether any masked bit is set; (value & mask) == mask tests whether all masked bits are set. With a zero mask, the first condition is false and the second is true. Keep the parentheses: == and != have higher precedence than &.
Do not confuse & with &&: built-in && produces bool and skips its right operand when the left operand is false. & provides no such short-circuiting. Unary &value, which takes an address, is a different operator. This description covers built-in &; overloads for class types can define different behavior.
The example targets compilation with -std=c++23; the production runtime is not attested. The eel.is source is an evolving working draft, not a frozen C++23 publication.
Examples
#include <iostream>
int main() {
constexpr unsigned value = 0b1101u;
constexpr unsigned mask = 0b0110u;
constexpr unsigned selected = value & mask;
static_assert(selected == 0b0100u);
std::cout << selected << '\n';
std::cout << ((value & mask) != 0u) << '\n';
std::cout << ((value & mask) == mask) << '\n';
}
Result
4 1 0
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.