ProgrammingHard

Which bit trick isolates the lowest set bit of an unsigned n?

  1. A. n & (n - 1)
  2. B. n & (~n + 1), which equals n & -n for unsigned n
  3. C. (n << 1) | n
  4. D. n ^ (n * 2)

Correct answer: B. n & (~n + 1), which equals n & -n for unsigned n

n & -n keeps only the lowest set bit. n & (n - 1) clears that bit instead, which is the other common trick.

More C Programming questions

Exams that ask this topic