The C standard library has no dedicated bitwise operations library. The operations are built into the language as the operators &, |, ^, ~, << and >>, which act on the individual bits of integer values. This guide focuses on the practical recipes built from them: parity checks, flag sets, bit extraction, enumerating subsets and safe shifting.
If you only need the operator reference, see the companion article on the C bitwise operators. Here the aim is to show what to do with them and which traps to avoid.
Each operator works on every bit position of its operands at once. For unsigned types the result is exactly what the truth tables say. For signed types the result also depends on the representation, which C23 fixed as two's complement.
Notice the cast in (uint8_t)~a. Operands smaller than int are promoted to int first, so without the cast ~a would be 0xFFFFFFF9.
The lowest bit decides whether an integer is odd, and n & (n - 1) clears the lowest set bit, which makes power-of-two tests a single expression.
is_even also works for negative numbers on two's complement machines. A compiler produces the same code as n % 2 == 0 here, so choose whichever is clearer to read.
Flag sets pack many booleans into one integer. Give each flag its own bit and combine them with OR.
The pattern is always the same: |= sets, &= ~ clears, ^= toggles and & tests.
When several small values share one integer, shift to the right position and mask the width. This is explicit and portable, unlike compiler-dependent bit-fields.
A mask with n bits represents a subset of n items. Counting from 0 to (1u << n) - 1 visits every subset, which is a standard technique in competitive programming and small combinatorial searches.
| Situation | Result in C |
|---|---|
| Shift count is negative | Undefined behavior |
| Shift count is greater than or equal to the promoted type width | Undefined behavior |
| Left shift of a negative signed value | Undefined behavior |
| Left shift that overflows a signed type | Undefined behavior |
| Right shift of a negative signed value | Implementation-defined (usually arithmetic) |
| Shifts on unsigned types within range | Fully defined, wraps modulo 2 to the power of the width for << |
Use unsigned types, validate variable shift counts, and enable -fsanitize=undefined (GCC and Clang) in tests so that bad shifts are caught at run time.
Swapping with a ^= b; b ^= a; a ^= b; is a popular puzzle answer. It fails when both operands refer to the same object, because the value becomes zero, and it is not faster on modern compilers, which handle a temporary variable with no extra memory access. Use a temporary.
No header is needed for the operators. For counting bits, C23 provides <stdbit.h>, and fixed-width types come from <stdint.h>.
No. They accept only integer types. To inspect the bits of a float, copy it into a uint32_t with memcpy.
A logical shift fills with zeros and is what unsigned types use. An arithmetic shift copies the sign bit and is what most compilers do for signed types, though the standard leaves it implementation-defined.
Rarely in a way that matters. Compilers already turn multiplication by powers of two into shifts, so use bitwise operators for bit logic, not as a manual optimisation.
Bitwise operations in C are language operators, not a library. Use unsigned fixed-width types, build masks with 1u << n, and remember the four core patterns: |= to set, &= ~ to clear, ^= to toggle and & to test. Watch for integer promotion and never shift by the type width or by a negative amount.