Every integer has actually an identical depiction in decimal and also binary. Except for 0 and also 1, the binary depiction of an integer has more digits than its decimal equivalent. To uncover the number of binary digits (bits) matching to any type of provided decimal integer, you can convert the decimal number to binary and also count the bits. For instance, the two-digit decimal integer 29 converts to the five-digit binary integer 11101. But there’s a method to compute the number of bits straight, without the conversion.

You are watching: How many bits would you need if you wanted to have the ability to count up to 1000?

Sometimes you desire to understand, not exactly how many type of bits are compelled for a specific integer, however exactly how many kind of are compelled for a d-digit integer — a range of integers. A selection of integers has actually a variety of bit counts. For instance, four-digit decimal integers need between 10 and 14 bits. For any type of d-digit selection, you might want to know its minimum, maximum, or average variety of bits. Those values deserve to be computed straight as well.

In this write-up, I will certainly present you those calculations. I will certainly be mentioning pure binary and also decimal, not computer system encodings like two’s enhance, fixed-suggest, floating-suggest, or BCD. All of the discussion assumes positive integers, although it uses to negative integers if you temporarily neglect their minus signs. 0 is a one-of-a-kind instance not spanned by the formulas, however obviously it has only 1 bit.

(I use the terms decimal integer and binary integer as soon as I really expect “an integer expressed in decimal numerals” and also “an integer expressed in binary numerals”. An integer is an integer, independent of its base.)

## Number of Bits in a Specific Decimal Integer

A positive integer n has actually b bits as soon as 2b-1 ≤ n ≤ 2b – 1. For example:

29 has 5 bits because 16 ≤ 29 ≤ 31, or 24 ≤ 29 ≤ 25 – 1123 has actually 7 bits bereason 64 ≤ 123 ≤ 127, or 26 ≤ 123 ≤ 27 – 1967 has 10 bits because 512 ≤ 967 ≤ 1023, or 29 ≤ 967 ≤ 210 – 1

For bigger numbers, you can consult a table of powers of 2 to discover the consecutive powers that contain your number.

To check out why this functions, think of the binary depictions of the integers 24 through 25 – 1, for instance. They are 10000 via 11111, all possible 5-little bit values.

### Using Logarithms

The above approach can be declared an additional way: the variety of bits is the exponent of the smallest power of 2 better than your number. You deserve to state that mathematically as:

bspec = ⌊log2(n)⌋ + 1

That formula has actually three parts:

log2(n) implies the logarithm in base 2 of n, which is the exponent to which 2 is elevated to acquire n. For example, log2(123) ≈ 6.9425145. The visibility of a fractional part means n is in between powers of two.+ 1 takes the exponent to the next better power of two. You deserve to think of this step as accounting for the 20th location of your binary number, which then provides you its total number of bits. For our example, that’s 6 + 1 = 7.

You could be tempted to use the ceiling feature — ⌈x⌉, which is the smallest integer better than or equal to x — to compute the number of bits as such:

bspec = ⌈log2(n)⌉

However before, this fails once n is a power of 2.

## Number of Bits in a d-Digit Decimal Integer

A positive integer n has d decimal digits as soon as 10d-1 ≤ n ≤ 10d – 1. How many type of bits execute numbers in this variety require? It varies. For example, think about four-digit decimal integers. Using the above formula you’ll check out that the smallest four-digit number, 1000, requires 10 bits, and also the largest four-digit number, 9999, calls for 14 bits. The number of bits varies in between those extremes. For example, 1344 needs 11 bits, 2527 requires 12 bits, and 5019 needs 13 bits. Why does this occur? Due to the fact that that single power of ten variety spans all or component of 5 consecutive power-of-2 arrays. Here’s how the examples look from that viewpoint:

1000 has actually 10 bits bereason 512 ≤ 1000 ≤ 1023, or 29 ≤ 1000 ≤ 210 – 11344 has 11 bits bereason 1024 ≤ 1344 ≤ 2047, or 210 ≤ 1344 ≤ 211 – 12527 has actually 12 bits because 2048 ≤ 2527 ≤ 4095, or 211 ≤ 2527 ≤ 212 – 15019 has 13 bits because 4096 ≤ 5019 ≤ 8191, or 212 ≤ 5019 ≤ 213 – 19999 has 14 bits bereason 8192 ≤ 9999 ≤ 16383, or 213 ≤ 9999 ≤ 214 – 1

This diagram reflects the ranges: Number of Bits In Four-Digit Decimal Integers

### Minimum Number of Bits in a d-Digit Integer

The minimum variety of bits compelled for a d-digit integer is computed simply by making use of the particular number formula on the minimum d-digit value:

bmin = ⌊log2(10d-1)⌋ + 1

We have the right to make this an extra effective computation by utilizing the logarithmic identity loga(xy) = y·loga(x):

bmin = ⌊log2(10d-1)⌋ + 1 = ⌊(d-1)·log2(10)⌋ + 1

In this form, we take the logarithm of a little continuous rather of a huge variable.

Due to the fact that we are handling powers of ten we have the right to use the ceiling attribute here (as long as d > 1); there is no positive power of ten that is likewise a power of 2. Here’s the equivalent formula:

bmin = ⌈log2(10d-1)⌉ = ⌈(d-1)·log2(10)⌉

### Maximum Number of Bits in a d-Digit Integer

The maximum variety of bits required for a d-digit integer is computed ssuggest by making use of the particular number formula on the maximum d-digit value:

bmax = ⌊log2(10d – 1)⌋ + 1

We can’t make the very same simplification as for the minimum value, at leastern not on the confront of it. But notice that ⌊log2(10d – 1)⌋ = ⌊log2(10d)⌋, because a power of ten and that power of ten minus one are both in the exact same power of two variety. (A power of ten minus one cannot be a power of two — it’s odd). This allows us to use this even more computationally reliable formula to the same effect:

bmax = ⌊log2(10d)⌋ + 1 = ⌊d·log2(10)⌋ + 1

And aget, for d > 1, we can use the ceiling feature instead:

bmax = ⌈d·log2(10)⌉

### Median Number of Bits in a d-Digit Integer

The average variety of bits compelled for a d-digit integer is the complete number of bits required to reexisting all d-digit integers divided by the variety of d-digit integers. For our example, the average is

bavg = (24·10 + 1024·11 + 2048·12 + 4096·13 + 1808·14)/9000 ≈ 12.74

## Computing The Formulas

Of the over formulas, these 2 — in these develops — are the many commonly used:

bspec = ⌊log2(n)⌋ + 1bmax = ⌈d·log2(10)⌉

How carry out you compute them?

First we need to figure out how to take the base-2 logarithm of a number. Many type of programming settings do not have actually a base-2 logarithm feature. You have the right to attend to that by doing a change of base:

log2(n) = loga(n)/loga(2)

a’ can recurrent any type of base; for instance, base e, the organic logarithm. For simplicity, I’ll drop the ‘a’ and refer to the logarithm attribute generically as log(n):

log2(n) = log(n)/log(2)

### Computing bspec

Let’s restate the formula in the create you would most likely enter it in a computer:

bspec = floor(log(n)/log(2)) + 1For example, the decimal integer 1,997,443,410 has actually floor(log(1997443410)/log(2)) + 1 = 31 bits.

You have to be cautious once computer logarithms; floating-point inaccuracies can cause an incorrect outcome, especially at power-of-two limits.

(If you are utilizing a language choose C you can protect against readjust of base — and enhance performance and also accuracy — by computing the floor of the base-2 logarithm using “Bit Twiddling Hacks”.)

For very huge numbers, you’ll need arbitrary-precision arithmetic. I use PARI/GP, for instance.

See more: 72' Deep Above Ground Pool With Deep Liner, 12' X 24' Oval 48

### Computing bmax

Let’s restate the formula for computer evaluation:

bmax = ceil(d*(log(10)/log(2)))(The parentheses roughly the department are unnecessary; I simply favor reasoning of log(10)/log(2) as a sepaprice consistent.)

bmax = ceil(d*3.32)

For example, a 16-digit decimal integer needs approximately ceil(16*3.32) = 54 bits.

You have to be mindful via this though; for example, ceil(25*3.32) = 83 (because 25*3.32 = 83), but 25-digit integers need 84 bits. You have to specify the consistent through even more precision to get the correct outcome. (Here’s one place wright here this error is made.)