*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 10

^{d-1}≤

*n*≤ 10

^{d}– 1. For example, 376 has

**3**digits because 100 ≤ 376 ≤ 999, or 10

^{2}≤ 376 ≤ 10

^{3}– 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:

*d*_{spec} = **⌊log _{10}(n)⌋ + 1**

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

**log**means the_{10}(*n*)of*logarithm in base 10**n*, which is the exponent to which 10 is raised to get*n*. For example, log_{10}(376) ≈ 2.575. The presence of a fractional part means*n*is between powers of ten.**⌊**is the*x*⌋of*floor**x*, which is the integer part of x. For example, ⌊2.575⌋ = 2. You can think of ⌊log_{10}(*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 10^{0}th 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:

*d*_{spec} = ⌈log_{10}(*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 2

^{b-1}≤

*n*≤ 2

^{b}– 1. How many digits do numbers in this range require? It will vary, depending on whether there is a power of ten between 2

^{b-1}and 2

^{b}– 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 10^{1} (10) occurs between 2^{3} (8) and 2^{4} – 1 (15). On the other hand, all 5-bit integers require two digits, since no power of ten occurs between 2^{4} (16) and 2^{5} – 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:

*d*_{min} = ⌊log_{10}(2^{b-1})⌋ + 1

We can make this a more efficient computation by using the logarithmic identity log_{a}(x^{y}) = y·log_{a}(x):

*d*_{min} = ⌊log_{10}(2^{b-1})⌋ + 1 = **⌊(b-1)·log _{10}(2)⌋ + 1**

In this form, we take the logarithm of a small constant instead of a large variable. (log_{10}(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:

*d*_{min} = ⌈log_{10}(2^{b-1})⌉ = **⌈(b-1)·log _{10}(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:

*d*_{max} = ⌊log_{10}(2^{b} – 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 ⌊log_{10}(2^{b} – 1)⌋ = ⌊log_{10}(2^{b})⌋, 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:

*d*_{max} = ⌊log_{10}(2^{b})⌋ + 1 = **⌊b·log _{10}(2)⌋ + 1**

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

*d*_{max} = **⌈b·log _{10}(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 log_{2}(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:

Change the base of the denominator:

Replace log_{10}(10) with 1:

Simplify:

This means there are approximately 0.3 digits per bit.