LOGBOOK

HELP

Quiz Entry - updated: 2026.07.07

How do you set, clear, and toggle specific bits?

OR sets a bit (x |= 1<<n), AND with the inverted mask clears it (x &= ~(1<<n)), and XOR toggles it (x ^= 1<<n).

// Set bit n
x |= (1 << n);

// Clear bit n
x &= ~(1 << n);

// Toggle bit n
x ^= (1 << n);

// Check if bit n is set
if (x & (1 << n)) { /* bit is set */ }

Example: Manipulate bit 3 of x = 0x10:

Set:    0x10 | 0x08 = 0x18
Clear:  0x10 & ~0x08 = 0x10 (already clear)
Toggle: 0x10 ^ 0x08 = 0x18

From Quiz: REVE1 / Number Representations | Updated: Jul 07, 2026