Bits are the lowest level you can work at in C.
Direct answer
Bitwise operators act on the individual bits of integers. & is AND, | is OR, ^ is XOR, ~ is NOT and << and >> shift bits left and right. They are used for flags, masks, compact data and low-level programming.
Examples
#include <stdio.h>
int main(void) {
unsigned int a = 5; // 0101
unsigned int b = 3; // 0011
printf("%u\n", a & b); // 1 (0001)
printf("%u\n", a | b); // 7 (0111)
printf("%u\n", a ^ b); // 6 (0110)
printf("%u\n", a << 1); // 10 (1010)
return 0;
}
Common patterns
flags |= (1u << 3); // set bit 3
flags &= ~(1u << 3); // clear bit 3
flags ^= (1u << 3); // toggle bit 3
if (flags & (1u << 3)) { /* bit 3 is set */ }
Cautions
- Use unsigned types to avoid surprises with signs.
- Do not confuse
&with&&.
Where Teyro fits
Teyro's C for Beginners course turns this into short lessons you can play in a few minutes a day. It has 78 lessons, and you can start free.
The bottom line
Learn the four patterns above and you can handle most bit tasks.



