计算存储十进制数所需的位 [英] Calculating bits required to store decimal number
问题描述
这是我坚持的作业问题。
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 containing:
i) 3 digits ii) 4 digits iii) 6 digits iv) n digits
我知道无符号整数将是0到2 ^ n,但我不知道表示一个数字所需的位数如何取决于它。请帮帮我。
I know that the range of the unsigned integer will be 0 to 2^n but I don't get how the number of bits required to represent a number depends upon it. Please help me out.
谢谢。
推荐答案
好,您只需要计算每种情况的范围并找到比该范围高的2的最低幂。
Well, you just have to calculate the range for each case and find the lowest power of 2 that is higher than that range.
例如,在i)中,3个小数位-> 10 ^ 3 = 1000个可能的数字,因此您必须找到2的最低幂,该幂大于1000,在这种情况下为2 ^ 10 = 1024(10位)。
For instance, in i), 3 decimal digits -> 10^3 = 1000 possible numbers so you have to find the lowest power of 2 that is higher than 1000, which in this case is 2^10 = 1024 (10 bits).
编辑:基本上,您需要找到具有数字位数的可能数字,然后找到数字位数(在另一个基数中,本例中为2,
Basically you need to find the number of possible numbers with the number of digits you have and then find which number of digits (in the other base, in this case base 2, binary) has at least the same possible numbers as the one in decimal.
要在给定位数的情况下计算可能性的数目: possibilities = base ^ ndigits
To calculate the number of possibilities given the number of digits: possibilities=base^ndigits
因此,如果小数点后3位(以10为底),则 10 ^ 3 = 1000
的可能性。然后,您必须找到一些二进制数字(位,以2为基数),以便可能性数至少为1000,在这种情况下为 2 ^ 10 = 1024
(9位数字是不够的,因为 2 ^ 9 = 512
小于1000)。
So, if you have 3 digits in decimal (base 10) you have 10^3=1000
possibilities. Then you have to find a number of digits in binary (bits, base 2) so that the number of possibilities is at least 1000, which in this case is 2^10=1024
(9 digits isn't enough because 2^9=512
which is less than 1000).
如果概括一下,您将具有: 2 ^ nbits = possibilities< => nbits = log2(可能性)
If you generalize this, you have: 2^nbits=possibilities <=> nbits=log2(possibilities)
应用于i)的结果为: log2(1000)= 9.97
,并且由于位数必须是整数,因此您必须将其舍入为10。
Which applied to i) gives: log2(1000)=9.97
and since the number of bits has to be an integer, you have to round it up to 10.
这篇关于计算存储十进制数所需的位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!