33

I am going to teach a small group of people about the numbering systems in computing and was wondering how many bits per digit are there in the decimal system, for instance:

  • Hex (base 16) - 4 bits
  • Octal (base 8) - 3 bits
  • Binary (base 2) - 1 bit
  • Decimal (base 10) - ?
user92592
  • 457
  • 1
  • 4
  • 6

10 Answers10

102

What you are looking for is the 2-based logarithm of 10, which is an irrational number of around 3.32192809489....

The fact that you can't use an integer number of bits for a decimal digit is the root cause of why many fractions that are easy to express in the decimal system (e.g. 1/5 or 0.2), are impossible (not hard: really impossible) to express in binary. This is important when evaluating rounding errors in floating point arithmetics.

Eugen Rieck
  • 20,637
21

In other words, what amount of information is contained in a single digit in these systems.

For base 2, base 4, base 8, base 16 and other 2N bases the answer is obvious because in a base 2N each digit can be expressed with exactly N digits.

How do you get N given 2N? Well, you use a 2-based logarithm, which is an inverse of exponentiation.

  • log2 2 = 1 (1 bit per digit in base 2)
  • log2 4 = 2 (2 bits per digit in base 4)
  • log2 8 = 3 (3 bits per digit in base 8)
  • log2 16 = 4 (4 bits per digit in base 16)

K-based logarithms of numbers that are not powers of K aren't cardinal numbers. In particular:

  • log2 10 = 3.321928094887362347870319429489390175864831393024580612054…

This number may look confusing, but it actually has some uses. For example, it's an entropy of a single decimal digit.

For your case, though, I don't think this value is of any use. @Christian's answer does a good job at explaining why.

gronostaj
  • 58,482
8

On the subject of bits:

I'm sorry to say the question is misguided. You wouldn't use bits in that manner. A bit is a binary digit. You can convert the decimal number 10, to a binary 1010 (8+2), so you'd need 4 bits to express the decimal value 10.


Powers of 2

You've fallen into a bit of a trap, by using binary (2), octal (8) and hexadecimal (16) as examples, because these are all powers of 2, and thus you can think of them in terms of bits, whereas 10 isn't a power of 2, so it just doesn't work very well like that.

Christian
  • 247
7

BCD - Binary Coded Decimal uses 4 bits per digit, the same as Hexadecimal.

https://en.wikipedia.org/wiki/Binary-coded_decimal

3

Using bits implies a power of 2, thus, as others have said you can't easily shohorn 10 bits into bytes without wastage. A common solution is to use 4 bits as per hexadecimal and waste the 6 states represented as A-F. The interesting bit is doing decimal math with this - it's not neat and simple.

A useful teaching idea might be to compare how Micky Mouse might have developed a counting system, as he only has 4 fingers per hand - which leads naturally to an octal based system.

davidgo
  • 73,366
3

In base 1024, each symbol is 10 bits. Three decimal digits have the same amount of information as one digit in base 1000, which is slightly less than 1024. Therefore, a decimal digit has slightly less than 10/3 bits. This approximation gives 3.333333..., while the exact number is 3.321928...

3

This might be an oversimplification but it depends on which question you are asking.
(and the answer is basically octal or hex)

I also don't consider fractional bits as bits because in practical usage bits don't have fractions.

Q1: How many bits can you represent in a decimal digit?

A1: You can represent 3 bits of information in a single decimal digit:

The most common scheme would be straight binary with wrapping where 0=8=000 and 1=9=001. But you could use any scheme there is nothing that says this is the only way to encode bits into decimal digits.

  • 0: 000
  • 1: 001
  • 2: 010
  • 3: 011
  • 4: 100
  • 5: 101
  • 6: 110
  • 7: 111
  • 8: 000 <- wrapping (or unused)
  • 9: 001 <- wrapping (or unused)

or

Q2: How many bits does it take to represent a decimal digit?

A2: You need at least 4 bits to represent all decimal digits. With some waste or wrapping.

Again the most common scheme would be straight binary with wrapping but you could use any other scheme.

  • 0: 0000
  • 1: 0001
  • 2: 0010
  • 3: 0011
  • 4: 0100
  • 5: 0101
  • 6: 0110
  • 7: 0111
  • 8: 1000
  • 9: 1001
  • 0: 1010 <- wrapping (or unused)
  • 1: 1011 <- wrapping (or unused)
  • 2: 1100 <- wrapping (or unused)
  • 3: 1101 <- wrapping (or unused)
  • 4: 1110 <- wrapping (or unused)
  • 5: 1111 <- wrapping (or unused)
2
  • Hex (base 16) - 4 bits
  • Octal (base 8) - 3 bits
  • Binary (base 2) - 1 bit
  • Decimal (base 10) - 3 1/3 bits.
    210 = 1,024
    103 = 1,000
    220 = 1,048,576
    106 = 1,000,000
    3 digits in base 10 up to 999 can be held in 10 bits in base 2.
    6 digits in base 10 up to 999,999 can be held in 20 bits in base 2.
    This is were the idea of kilobytes, megabytes, and gigabytes originated.
0

Disclaimer - I'm not an information theorist, just a code monkey who works primarily in C and C++ (and thus, with fixed-width types), and my answer is going to be from that particular perspective.

It takes on average 3.2 bits to represent a single decimal digit - 0 through 7 can be represented in 3 bits, while 8 and 9 require 4. (8*3 + 2*4)/10 == 3.21.

This is less useful than it sounds. For one thing, you obviously don't have fractions of a bit. For another, if you're using native integer types (i.e., not BCD or BigInt), you're not storing values as a sequence of decimal digits (or their binary equivalents). An 8 bit type can store some values that take up to 3 decimal digits, but you can't represent all 3-decimal-digit values in 8 bits - the range is [0..255]. You cannot represent the values [256..999] in only 8 bits.

When we're talking about values, we'll use decimal if the application expects it (e.g., a digital banking application). When we're talking about bits, we'll usually use hex or binary (I almost never use octal since I work on systems that use 8-bit bytes and 32-bit words, which aren't divisible by 3).

Values expressed in decimal don't map cleanly on to binary sequences. Take the decimal value 255. The binary equivalents of each digit would be 010, 101, 101. Yet, the binary representation of the value 255 is 11111111. There's simply no correspondence between any of the decimal digits in the value to the binary sequence. But there is a direct correspondence with hex digits - F == 1111, so that value can be represented as FF in hex.

If you're on a system where 9-bit bytes and 36-bit words are the norm, then octal makes more sense since bits group naturally into threes.


  1. Actually, the average per digit is smaller since 0 and 1 only require a single bit, while 2 and 3 only require 2 bits. But, in practice, we consider 0 through 7 to take 3 bits. Just makes life easier in a lot of ways.

John Bode
  • 117
0

If I were teaching this, I would first explain what a number (expressed as a series of digits means). i.e., from right to left, assuming base n, a * n^0 + b * n^1 + c * n ^2 ... z * n^y.

Then explain that 10^3 is approximately equal to 2 ^ 10. It is not exact and is the reason in computers, we often do not know what 2k really means (is it 2,000 or 2,048?) It serves fairly well for quick approximations. 2^16 is about 2 ^ (16 - 10) * 1,000, or 2^6 (64) * 1,000 or 64,000. In reality, it is 65,536, but if you don't mind being off around a percent, it works fairly well for quick approximations.

PeterH
  • 7,595