Search code examples
c++numbersradix

Determine the number of digits of number in base 2 when you know the number in base 10


Someone asked me to determine how many digits does a number in base 2 has, when you know the number in base 10.

Is there any method to determine the number of digits of the number in base 2 without converting the number from base 10 to base 2?

At this moment I know only how to determine the number of digits of a number when converted to base 2, only if I compute the number in base 2.

Thank you!


Solution

  • I may be wrong, but since a binary number of n bits can represent a maximum decimal number of 2n - 1, I would guess that a decimal number k would occupy at most log2k + 1 bits.