Skip to content
AgileLogics
4 min read

Bit Manipulation

Prerequisites: String manipulation in depth

Bitwise operators work directly on a value's individual bits — used for compact flag sets, low-level protocols, and performance-critical code.

#define FLAG_READ  (1 << 0) // 0b0001
#define FLAG_WRITE (1 << 1) // 0b0010
#define FLAG_EXEC  (1 << 2) // 0b0100

int permissions = FLAG_READ | FLAG_WRITE; // set two flags: 0b0011

if (permissions & FLAG_WRITE) {
    printf("Can write\n");
}

permissions &= ~FLAG_WRITE; // clear the write flag: 0b0001

<< and >> shift bits left/right (each left shift by 1 doubles the value, for a non-negative int); &, |, ^, ~ are AND, OR, XOR, and NOT.