Calculating bits required to store decimal number

后端 未结 12 807
自闭症患者
自闭症患者 2020-12-23 22:21

This is a homework question that I am stuck with.

Consider unsigned integer representation. How many bits will be required to store a decimal number

12条回答
  •  感动是毒
    2020-12-23 23:07

    The largest number that can be represented by an n digit number in base b is bn - 1. Hence, the largest number that can be represented in N binary digits is 2N - 1. We need the smallest integer N such that:

    2N - 1 ≥ bn - 1
    ⇒ 2N ≥ bn

    Taking the base 2 logarithm of both sides of the last expression gives:

    log2 2N ≥ log2 bn
    ⇒ N ≥ log2 bn
    ⇒ N ≥ log bn / log 2

    Since we want the smallest integer N that satisfies the last relation, to find N, find log bn / log 2 and take the ceiling.

    In the last expression, any base is fine for the logarithms, so long as both bases are the same. It is convenient here, since we are interested in the case where b = 10, to use base 10 logarithms taking advantage of log1010n == n.

    For n = 3:

    N = ⌈3 / log10 2⌉ = 10

    For n = 4:

    N = ⌈4 / log10 2⌉ = 14

    For n = 6:

    N = ⌈6 / log10 2⌉ = 20

    And in general, for n decimal digits:

    N = ⌈n / log10 2⌉

提交回复
热议问题