Bitwise Operators
C gives direct access to an integer's bit pattern: | sets bits, & tests or clears them, ^ toggles them, and << / >> shift bits left or right. 1 << n produces a value with only bit n set — a common pattern for representing one flag out of many in a single integer.
#include <stdio.h>
int main() {
unsigned int flags = 0;
flags |= 1 << 0;
flags |= 1 << 2;
printf("%u\n", flags);
printf("%d\n", (flags & (1 << 2)) != 0);
printf("%d\n", (flags & (1 << 1)) != 0);
return 0;
}5 1 0
flags |= 1 << 0 and flags |= 1 << 2 set bits 0 and 2, producing 5 (binary 101). (flags & (1 << 2)) != 0 then tests whether bit 2 specifically is set, independent of any other bit — the same pattern operating systems, embedded code, and network protocols use to pack many boolean flags into a single integer.
Next: enums and typedef — naming constants and giving complex types shorter names.
Practice exercise
Set an unsigned int flags to 0, then set bit 3 using |= and (1 << 3). Print whether bit 3 is set using (flags & (1 << 3)) != 0.