Math Fundamentals18 sections · 814 units
Open in Course

Clearing a Bit

Turn a bit off

To clear bit kk (make it 0) in number nn, you AND nn with the complement of 2k2^k. In code: n&(1<<k)n \& \sim(1 << k).

Why? The expression (1<<k)\sim(1 << k) creates a number with all bits set to 1 except bit kk, which is 0. ANDing with this mask turns off bit kk while preserving all others.

Example: Clear bit 2 in 13=110113 = 1101. Compute 13&4=1101&1011=1001=913 \& \sim4 = 1101 \& 1011 = 1001 = 9. Bit 2 is now off.