Number of bits in 0

977 Views Asked by At

I have found a few sources which have said that the number of bits in a binary number is equal to

$floor(log_2(n))+1$.

But this doesn't seem to work for binary values less than 1. I would expect the value 0 to have zero bits. But plugging in 0 for n is not in the domain of the function. Stranger results are given by plugging in values like 0.2 which give -2 bits. This doesn't sound right. So I created this equation:

$ceil(log_2(n+1))$.

It has the same output as the previous function for all $n>=1$. It even says 0 is 0 bits, and 0.2 is 1 bit, as I would expect.

Why do people seem to like the first function more? Can someone explain why the number of bits in 0 is undefined in terms of Shannon information?

EDIT:

Here are a the sources I drew from:

https://www.exploringbinary.com/number-of-bits-in-a-decimal-integer/

Given a number, how to find the length of its binary representation?

MathWorld states the first equation is equal to the second, but ignores values less than 1. http://mathworld.wolfram.com/BitLength.html

EDIT 2:

I'm looking for a strict mathematical explanation for the number of bits in 0 in terms of information theory. The two equations above give different answers, so which is correct and why? To clarify, I don't mean the physical length of '0' or the length of the string that represents '0', but the value zero itself.

According to Mathematica,

"BitLength[n] gives the number of binary bits necessary to represent the integer n"

and executing this command,

BitLength[0] -> 0

but even the Mathematica documentation and MathWorld fail to give a reason for this.

1

There are 1 best solutions below

0
On

The formulas you quote are for the number of bits in the binary expression of a positive integer. As you say, the first is undefined for $0$ and the second gives $0$ bits. I would say both are wrong for $0$ because it takes one binary digit (note I did not say bit) to express $0$, but those formulas are intended only for positive integers. They agree for all of those.

This is different from the number of bits for $0$ in information theory. In information theory you need to consider the number of possibilities. If you have $256$ equally probable values for a quantity, any one of them is $8$ bits, whether it is $0$ or $234$.