Number of Decimal Digits In a Binary Integer

This is a companion article to “Number of Bits in a Decimal Integer”

If you have an integer expressed in decimal and want to know how many bits are required to express it in binary, you can perform a simple calculation. If you want to know how many bits are required to express a d-digit decimal integer in binary, you can perform other simple calculations for that.

What if you want to go in the opposite direction, that is, from binary to decimal? There are similar calculations for determining the number of decimal digits required for a specific binary integer or for a b-bit binary integer. I will show you these calculations, which are essentially the inverses of their decimal to binary counterparts.

(As in the companion article, I will be discussing only pure binary and decimal numbers, and dealing with positive integers only.)

Number of Digits in a Specific Binary Integer

If I gave you a binary integer and asked you how many decimal digits it requires, you would convert it to decimal and count the digits. But a computer program does not need to do it that way, since it works in binary arithmetic. It can compute the number of digits directly, without converting the integer to decimal. (Although we’ll be talking about arithmetic operations on binary numbers, I will use decimal numerals in my description.)

A positive integer n has d digits when 10d-1n ≤ 10d – 1. For example, 376 has 3 digits because 100 ≤ 376 ≤ 999, or 102 ≤ 376 ≤ 103 – 1. Said another way, the number of digits in n is the exponent of the smallest power of ten greater than n; mathematically, that’s stated as:

dspec = ⌊log10(n)⌋ + 1

Let’s see how the formula works by looking at its three parts:

  • log10(n) means the logarithm in base 10 of n, which is the exponent to which 10 is raised to get n. For example, log10(376) ≈ 2.575. The presence of a fractional part means n is between powers of ten.
  • x is the floor of x, which is the integer part of x. For example, ⌊2.575⌋ = 2. You can think of ⌊log10(n)⌋ as the exponent of the highest power of ten in the decimal representation of n.
  • + 1 takes the exponent to the next higher power of ten. You can think of this step as accounting for the 100th place of your decimal number, which then gives you its total number of digits. For our example, that’s 2 + 1 = 3.

Don’t be tempted to use the ceiling function — ⌈x⌉, which is the smallest integer greater than or equal to x — to compute the number of digits as such:

dspec = ⌈log10(n)⌉

This fails when n is a power of ten.

Number of Digits in a b-Bit Binary Integer

A positive integer n has b bits when 2b-1n ≤ 2b – 1. How many digits do numbers in this range require? It will vary, depending on whether there is a power of ten between 2b-1 and 2b – 1. If there is no power of ten between them, all b-bit integers will convert to d-digit integers; if there is a power of ten between them, the first part of the b-bit range will require d digits, and the remaining part will require d+1 digits.

For example, 4-bit integers require either one or two digits, because 101 (10) occurs between 23 (8) and 24 – 1 (15). On the other hand, all 5-bit integers require two digits, since no power of ten occurs between 24 (16) and 25 – 1 (31).

I will refer to the two possible values as the minimum and maximum, even though they will be the same most of the time.

Minimum Number of Digits in a b-Bit Integer

The minimum number of digits required for a b-bit integer is computed simply by using the specific number formula on the minimum b-bit value:

dmin = ⌊log10(2b-1)⌋ + 1

We can make this a more efficient computation by using the logarithmic identity loga(xy) = y·loga(x):

dmin = ⌊log10(2b-1)⌋ + 1 = ⌊(b-1)·log10(2)⌋ + 1

In this form, we take the logarithm of a small constant instead of a large variable. (log10(2) is approximately 0.3, but you should compute it to more precision if you want proper results from this formula.)

Since we are dealing with powers of two we can use the ceiling function here (as long as b > 1); there is no positive power of two that is also a power of ten. Here’s the equivalent formula:

dmin = ⌈log10(2b-1)⌉ = ⌈(b-1)·log10(2)⌉

Maximum Number of Digits in a b-Bit Integer

The maximum number of digits required for a b-bit integer is computed simply by using the specific number formula on the maximum b-bit value:

dmax = ⌊log10(2b – 1)⌋ + 1

We can’t make the same simplification as for the minimum value, at least not on the face of it. But notice that ⌊log10(2b – 1)⌋ = ⌊log10(2b)⌋, since a power of two and that power of two minus one are both in the same power of ten range. (A power of two minus one cannot be a power of ten — it’s odd). This allows us to use this more computationally efficient formula to the same effect:

dmax = ⌊log10(2b)⌋ + 1 = ⌊b·log10(2)⌋ + 1

And again, for b > 1, we can use the ceiling function instead:

dmax = ⌈b·log10(2)⌉

Examples

Using the above two formulas, you’ll find that 32-bit integers require 10 digits, and 64-bit integers require either 19 or 20 digits.

Ratio of Decimal Digits to Bits

I showed you that the ratio of bits to digits converges to log2(10). You can derive the expression for the ratio of digits to bits similarly — or you can just recognize that it is the inverse of the bits to digits ratio:

\mbox{\footnotesize{\displaystyle{\frac{1}{log_{2}(10)}}}}

Change the base of the denominator:

\mbox{\footnotesize{\displaystyle{\frac{1}{\left(\frac{log_{10}(10)}{log_{10}(2)}\right)}}}}

Replace log10(10) with 1:

\mbox{\footnotesize{\displaystyle{\frac{1}{\left(\frac{1}{log_{10}(2)}\right)}}}}

Simplify:

\mbox{\footnotesize{\displaystyle{log_{10}(2)}}}

This means there are approximately 0.3 digits per bit.

Dingbat
RSS feed icon
RSS e-mail icon

Leave a Reply

Your email address will not be published. Required fields are marked *

(Cookies must be enabled to leave a comment...it reduces spam.)