LOGBOOK

HELP

Quiz Entry - updated: 2026.07.10

How do you flip (invert) all bits in a byte in C?

Apply ~, which flips every bit (0→1, 1→0) — but watch out: the operand is promoted to int first, so cast back to unsigned char if you want just one byte.

// 01000001
unsigned char b = 0x41;
// 10111110 = 0xBE
unsigned char flipped = ~b;

How it works:

  • ~ flips every bit: 0→1, 1→0
  • Applied to the entire value

Example with binary:

  0x41 = 01000001
 ~0x41 = 10111110 = 0xBE

Watch out for integer promotion:

unsigned char b = 0x41;
// Result is 0xFFFFFFBE (32-bit), not 0xBE!
int result = ~b;
// Cast back: (unsigned char)(~b) = 0xBE

From Quiz: REVE1 / C Programming | Updated: Jul 10, 2026