C Reference ยท Operators and Expressions
Bitwise operators in C
Reference for the C bitwise operators &, |, ^ and ~: truth tables, masks for setting, clearing, toggling and testing bits, promotion rules and signed pitfalls.
The bitwise operators work on the individual bits of integer values. & is AND, | is OR, ^ is exclusive OR (XOR) and ~ is complement (flip every bit). They are the foundation of flag sets, masks, hardware registers, checksums and compact data structures.
Each operator treats its operands as strings of bits after the usual conversions: small types are promoted to int first, and two different types are converted to a common type. The operators here never fail at run time, but mixing in signed or promoted values can produce surprising results. For shifting bits left and right, see shift operators.
unsigned flags = 0; flags |= 1u << 3; /* set bit 3 */ flags &= ~(1u << 3); /* clear bit 3 */
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 scanf or fgets), 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 scanf or fgets), it gets the next line from this box.
Syntax
a & b a | b a ^ b ~a
| Operator | Bit result | Typical use |
|---|---|---|
a & b |
1 where both bits are 1 | Test or keep bits (masking). |
a | b |
1 where either bit is 1 | Set bits. |
a ^ b |
1 where the bits differ | Toggle bits, compare, simple parity. |
~a |
every bit flipped | Build "all but" masks: a & ~mask clears bits. |
Operands: integer types only (not floating-point or pointers). Result: the common type after conversions. ~ promotes its operand first.
Errors / undefined behavior: none of these four operators has undefined behavior on its own. Bitwise operations on negative signed values depend on the representation (two's complement is guaranteed from C23).
Usage notes
- Precedence is a trap.
&,^and|rank below the comparison operators:flags & MASK == 0parses asflags & (MASK == 0). Write(flags & MASK) == 0. See operator precedence. - Order of precedence among them:
&first, then^, then|. Parenthesize anyway. - Use unsigned types (
1u << n,uint32_t) for masks. Signed operands lead to sign-extension and shifting issues. - Promotion:
~cfor anunsigned charis anintwith the top bits set. Cast back to the narrow type or mask the result:(unsigned char)~c. - Set, clear, toggle, test are
|= mask,&= ~mask,^= mask,& mask. - Do not confuse with logical operators.
&and|evaluate both sides and combine bits;&&and||short-circuit and yield 0 or 1 (logical operators). - Compound forms
&=,|=,^=are in assignment operators. - Compilers recognise idioms such as rotations and bit counts; with GCC you can also use
__builtin_popcount, and in C23 the<stdbit.h>functions (not available on this site's runner). - Bit-fields offer named flags in a struct, but their layout is implementation-defined. Masks are the portable option.