Data Structures & AlgorithmsModerateTCS NQT · CAT

Problem: How many bits are needed to write 10^9 in binary (the number of bits in its binary representation)?

  1. A. 29
  2. B. 10
  3. C. 32
  4. D. 30

Correct answer: D. 30

2²⁹ = 536870912 ≤ 10⁹ < 2³⁰, so the bit length is floor(log₂ n) + 1 = 30. Forgetting the + 1 gives 29, 10 is the decimal digit count, and 32 is the size of an int, not the bit length.

More Coding Interview Problems questions

Exams that ask this topic