Math Fun

How many digits are in a number n bits expressed in base b?

digits = n / (n / logb(2 ^ n))

In case your calculator doesn’t have a logb(n) function on it (mine doesn’t), use the version below.

digits = n / (n / (log(2 ^ n) / log(b)))

I’ve needed to know that several times and never took the time to figure it out or find it, but today I did. Isn’t that handy?

Here’s a little JavaScript calculator you can use to try it out.

Bits:
Base:
Digits:

One thought on “Math Fun”

  1. I just happened across your your website while searching for something completely unrelated. But I noticed your formula because I derived it myself an long time ago. I’d like to point out that it can be reduced to a simple multiplication by a constant that can be calculated once for any given base.

    Your formula is:

    digits = n / (n / logb(2 ^ n))

    This reduces right away to:

    digits = logb(2 ^ n)

    This can be simplified further to eliminate raising n to the power of 2
    if you rewrite it using the log2 (log base 2) function:

    digits = logb( 2 ^ n )
    = log2( 2 ^ n ) / log2( b )
    = n / log2( b )

    Notice that 1 / log2( b ) only has to be calculated once for a given base b.
    To use ln (natural log) or log (log base 10) use the formula:

    log2( x ) = ln( x ) / ln( 2 )
    = log( x ) / log( 2 )

    giving:

    digits = logb( 2 ^ n )
    = n ln( 2 ) / ln( b )
    = n log( 2 ) / log( b )

    In general, whenever you take the log (in any base) of a constant raised to a variable power x, the result will always be linearly proportional to x (x times a constant). I.e. loga(b^x) = cx where c = ln(b)/ln(a).

Comments are closed.