Math Fundamentals18 sections · 814 units
Open in Course

Common Operations Summary

Quick reference

Here are the four operations you'll use constantly:

• Check if bit kk is set: (n&(1<<k))0(n \& (1 << k)) \neq 0

• Set bit kk: n(1<<k)n | (1 << k)

• Clear bit kk: n&(1<<k)n \& \sim(1 << k)

• Toggle bit kk: n(1<<k)n \wedge (1 << k)

The pattern (1<<k)(1 << k) appears everywhere because it creates a number with only bit kk set. Memorize these four operations. You'll use them in almost every bit manipulation problem.