计算存储十进制数所需的位数

发布于 2024-11-30 20:06:19 字数 192 浏览 2 评论 0原文

考虑无符号整数表示。有多少位 需要存储包含以下内容的十进制数:

i) 3 digits ii) 4 digits iii) 6 digits iv) n digits

我知道无符号整数的范围是 0 到 2^n,但我不明白表示数字所需的位数如何取决于它。

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(13

风透绣罗衣 2024-12-07 20:06:20

那么,您只需计算每种情况的范围,并找到高于该范围的 2 的最低幂。

例如,在i)中,3位十进制数字-> 10^3 = 1000 个可能的数字,因此您必须找到高于 1000 的 2 的最低幂,在本例中为 2^10 = 1024(10 位)。

编辑:基本上,您需要找到可能的数字的数量以及您拥有的位数,然后找到哪些数字(在其他基数中,在本例中为基数 2,二进制)至少有与十进制中的数字相同的可能数字。

要计算给定位数的可能性数量:possibilities=base^ndigits

因此,如果您有 3 位十进制数字(以 10 为基数),则您有 10^3=1000 > 可能性。然后你必须找到一些二进制数字(位,基数为 2),使得可能性的数量至少为 1000,在本例中为 2^10=1024 (9 位数字不是还不够,因为 2^9=512 小于 1000)。

如果你概括这一点,你有:2^nbits=possibilities <=> nbits=log2(possibilities)

应用于 i) 给出: log2(1000)=9.97 并且由于位数必须是整数,因此必须将其四舍五入到 10 。

Well, you just have to calculate the range for each case and find the lowest power of 2 that is higher than that range.

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).

Edit: 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.

To calculate the number of possibilities given the number of digits: possibilities=base^ndigits

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).

If you generalize this, you have: 2^nbits=possibilities <=> nbits=log2(possibilities)

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.

恋竹姑娘 2024-12-07 20:06:20

存储 n 个整数(例如,0n - 1)所需的二进制位数的公式为:

loge(n) / loge(2)

并四舍五入。

例如,对于值 -128 到 127(有符号字节)或 0 到 255(无符号字节),整数的数量为 256,因此 n 为 256,从上面的公式得出 8。

对于0n,在上式中使用n + 1(有n + 1个整数)。

在计算器上,loge 可能仅标记为 logln(自然对数)。

The formula for the number of binary bits required to store n integers (for example, 0 to n - 1) is:

loge(n) / loge(2)

and round up.

For example, for values -128 to 127 (signed byte) or 0 to 255 (unsigned byte), the number of integers is 256, so n is 256, giving 8 from the above formula.

For 0 to n, use n + 1 in the above formula (there are n + 1 integers).

On your calculator, loge may just be labelled log or ln (natural logarithm).

王权女流氓 2024-12-07 20:06:20

b 为基数的 n 位数字可以表示的最大数字是 bn - 1。因此,可以用N个二进制数字表示的最大数字是2N - 1。我们需要最小的整数N,这样:

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

最后一个表达式两边取以 2 为底的对数,得到:

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

由于我们想要满足最后一个关系的最小整数N,所以要找到N,找到N >log bn / log 2 并获取上限。

在最后一个表达式中,任何底数都可以用于对数,只要两个底数相同即可。这里很方便,因为我们对 b = 10 的情况感兴趣,利用 log1010n< 来使用以 10 为底的对数/sup> == n

对于n = 3

N = ⌈3 / log10 2⌉ = 10

对于 n = 4

N = ⌈4 / log10 2⌉ = 14

对于 n = 6

N = ⌈6 / log10 2⌉ = 20

一般来说,对于 n 位十进制数字:

N = ⌈n / log10 2⌉

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⌉

渔村楼浪 2024-12-07 20:06:20

好吧,概括一下需要多少位来表示一个数字的技术就是这样完成的。您有 R 个符号用于表示,并且您想知道有多少位,请求解此方程 R=2^n 或 log2(R)=n。其中 n 是位数,R 是表示的符号数。

对于十进制数系统 R=9,因此我们求解 9=2^n,答案是每个十进制数字 3.17 位。因此,3 位数字需要 9.51 位或 10 位。1000 位数字需要 3170 位

Ok to generalize the technique of how many bits you need to represent a number is done this way. You have R symbols for a representation and you want to know how many bits, solve this equation R=2^n or log2(R)=n. Where n is the numbers of bits and R is the number of symbols for the representation.

For the decimal number system R=9 so we solve 9=2^n, the answer is 3.17 bits per decimal digit. Thus a 3 digit number will need 9.51 bits or 10. A 1000 digit number needs 3170 bits

最偏执的依靠 2024-12-07 20:06:20

假设问题是问存储

  1. 3 位数字

所需的最小位数是多少,我解决这个问题的方法是:

  • 我们需要存储的 3 位数字的最大位数是多少? Ans: 999
  • 存储这个数字最少需要多少位?

这个问题可以通过递归地将999除以2来解决。然而,使用数学的力量来帮助我们更简单。本质上,我们正在求解以下方程的 n:

2^n = 999
nlog2 = log999
n ~ 10

您需要 10 位来存储 3 位数字。

用类似的方法解决其他子问题!

希望这有帮助!

Assuming that the question is asking what's the minimum bits required for you to store

  1. 3 digits number

My approach to this question would be:

  • what's the maximum number of 3 digits number we need to store? Ans: 999
  • what's the minimum amount of bits required for me to store this number?

This problem can be solved this way by dividing 999 by 2 recursively. However, it's simpler to use the power of maths to help us. Essentially, we're solving n for the equation below:

2^n = 999
nlog2 = log999
n ~ 10

You'll need 10 bits to store 3 digit number.

Use similar approach to solve the other subquestions!

Hope this helps!

后eg是否自 2024-12-07 20:06:20

简短的回答是:

int nBits = ceil(log2(N));

这只是因为 pow(2, nBits) 比 N 稍大。

The short answer is:

int nBits = ceil(log2(N));

That's simply because pow(2, nBits) is slightly bigger than N.

星星的軌跡 2024-12-07 20:06:20

继续将该数字除以 2,直到商为 0。

Keep dividing the number by 2 until you get a quotient of 0.

半夏半凉 2024-12-07 20:06:20

最简单的答案是将所需的值转换为二进制,并查看该值需要多少位。然而,问题是问 X 位十进制数有多少位。在这种情况下,似乎您必须选择 X 位的最高值,然后将该数字转换为二进制。

作为一个基本示例,假设我们想要存储一个以 10 为基数的 1 位数字,并且想知道需要多少位。最大的1位十进制数是9,所以我们需要将其转换为二进制。得到 1001,总共 4 位。同样的示例可以应用于两位数(最大值为 99,转换为 1100011)。要求解 n 位数字,您可能需要求解其他数字并搜索模式。

要将值转换为二进制,请重复除以二,直到商为 0(所有余数将为 0 或 1)。然后反转余数的顺序以获得二进制数。

示例:13 转为二进制。

  • 13/2 = 6 r 1
  • 6/2 = 3 r 0
  • 3/2 = 1 r 1
  • 1/2 = 0 r >1
  • = 1101 ((8*1) + (4*1) + (2*0) + (1*1))

希望这对您有所帮助。

The simplest answer would be to convert the required values to binary, and see how many bits are required for that value. However, the question asks how many bits for a decimal number of X digits. In this case, it seems like you have to choose the highest value with X digits, and then convert that number to binary.

As a basic example, Let's assume we wanted to store a 1 digit base ten number, and wanted to know how many bits that would require. The largest 1 digit base ten number is 9, so we need to convert it to binary. This yields 1001, which has a total of 4 bits. This same example can be applied to a two digit number (with the max value being 99, which converts to 1100011). To solve for n digits, you probably need to solve the others and search for a pattern.

To convert values to binary, you repeatedly divide by two until you get a quotient of 0 (and all of your remainders will be 0 or 1). You then reverse the orders of your remainders to get the number in binary.

Exampe: 13 to binary.

  • 13/2 = 6 r 1
  • 6/2 = 3 r 0
  • 3/2 = 1 r 1
  • 1/2 = 0 r 1
  • = 1101 ((8*1) + (4*1) + (2*0) + (1*1))

Hope this helps out.

寻找一个思念的角度 2024-12-07 20:06:20

设其所需的 n 位,然后 2^n=(base)^digit,然后取 log 并计数。对于 n

let its required n bit then 2^n=(base)^digit and then take log and count no. for n

吃颗糖壮壮胆 2024-12-07 20:06:20

对于 n 位二进制数,它可以容纳的最大十进制值为

2^n - 1,并且 2^n 是使用这些数字可以生成的总排列。

考虑一个只需要三位数字的情况,即您的情况 1。我们看到要求是,

2^n - 1 >= 999

将 log 应用于两边,

log(2^n - 1) >= log( 999)

log(2^n) - log(1) >= log(999)

n = 9.964(大约)。

由于 9.964 不是有效位数,因此取 n 的上限值,我们得到 n = 10。

For a binary number of n digits the maximum decimal value it can hold will be

2^n - 1, and 2^n is the total permutations that can be generated using these many digits.

Taking a case where you only want three digits, ie your case 1. We see that the requirements is,

2^n - 1 >= 999

Applying log to both sides,

log(2^n - 1) >= log(999)

log(2^n) - log(1) >= log(999)

n = 9.964 (approx).

Taking the ceil value of n since 9.964 can't be a valid number of digits, we get n = 10.

乱了心跳 2024-12-07 20:06:20

这里有很多答案,但我会添加我的方法,因为我在解决同一问题时发现了这篇文章。

从我们所知道的从 0 到 16 的数字开始。

Number           encoded in bits         minimum number of bits to encode
0                000000                  1
1                000001                  1
2                000010                  2
3                000011                  2
4                000100                  3
5                000101                  3
6                000110                  3
7                000111                  3
8                001000                  4
9                001001                  4
10               001010                  4
11               001011                  4
12               001100                  4
13               001101                  4
14               001110                  4
15               001111                  4
16               010000                  5

查看中断,它显示了这个表格

number <=       number of bits
1               0
3               2
7               3
15              4

那么,现在我们如何计算该模式呢?

请记住,对数基数 2 (n) = 对数基数 10 (n) / 对数基数 10 (2)

number    logb10 (n)   logb2 (n)   ceil[logb2(n)] 
1         0            0           0           (special case)
3         0.477        1.58        2
7         0.845        2.807       3  
8         0.903        3           3           (special case)
15        1.176        3.91        4
16        1.204        4           4           (special case)
31        1.491        4.95        5
63        1.799        5.98        6

现在所需的结果与第一个表匹配。请注意某些值也是特殊情况。 0 和任何 2 的幂的数字。当您应用上限时,这些值不会改变,因此您知道需要加 1 才能得到
最小位域长度。

为了考虑特殊情况,请在输入中添加 1。用 python 实现的结果代码是:

from math import log
from math import ceil
def min_num_bits_to_encode_number(a_number):
    a_number=a_number+1  # adjust by 1 for special cases

    # log of zero is undefined
    if 0==a_number:
        return 0
    num_bits = int(ceil(log(a_number,2)))  # logbase2 is available
    return (num_bits)

There are a lot of answers here, but I'll add my approach since I found this post while working on the same problem.

Starting with what we know here are the number from 0 to 16.

Number           encoded in bits         minimum number of bits to encode
0                000000                  1
1                000001                  1
2                000010                  2
3                000011                  2
4                000100                  3
5                000101                  3
6                000110                  3
7                000111                  3
8                001000                  4
9                001001                  4
10               001010                  4
11               001011                  4
12               001100                  4
13               001101                  4
14               001110                  4
15               001111                  4
16               010000                  5

looking at the breaks, it shows this table

number <=       number of bits
1               0
3               2
7               3
15              4

So, now how do we compute the pattern?

Remember that log base 2 (n) = log base 10 (n) / log base 10 (2)

number    logb10 (n)   logb2 (n)   ceil[logb2(n)] 
1         0            0           0           (special case)
3         0.477        1.58        2
7         0.845        2.807       3  
8         0.903        3           3           (special case)
15        1.176        3.91        4
16        1.204        4           4           (special case)
31        1.491        4.95        5
63        1.799        5.98        6

Now the desired result matching the first table. Notice how also some values are special cases. 0 and any number which is a powers of 2. These values dont change when you apply ceiling so you know you need to add 1 to get
the minimum bit field length.

To account for the special cases add one to the input. The resulting code implemented in python is:

from math import log
from math import ceil
def min_num_bits_to_encode_number(a_number):
    a_number=a_number+1  # adjust by 1 for special cases

    # log of zero is undefined
    if 0==a_number:
        return 0
    num_bits = int(ceil(log(a_number,2)))  # logbase2 is available
    return (num_bits)
Bonjour°[大白 2024-12-07 20:06:20

这个有效!

floor(loge(n) / loge(2)) + 1

要包含负数,您可以添加额外的位来指定符号。

floor(loge(abs(n)) / loge(2)) + 2

This one works!

floor(loge(n) / loge(2)) + 1

To include negative numbers, you can add an extra bit to specify the sign.

floor(loge(abs(n)) / loge(2)) + 2
肩上的翅膀 2024-12-07 20:06:20

作为 excel/libreoffice 公式:

+----+-------------+--------------+
|..n.+.........2^n.+...LOG(2^n,2).|
+----+-------------+--------------+
|..1.+...........2.+...1..........|
|..4.+..........16.+...4..........|
|..8.+.........256.+...8..........|
|.16.+.......65536.+..16..........|
|.24.+....16777216.+..24..........|
|.30.+..1073741824.+..30..........|
|.31.+..2147483648.+..31..........|
|.32.+..4294967296.+..32..........|
+----+-------------+--------------+

因此,对于 A1=12345,=LOG(A1,2) 给出 14 (2^14 = 16384)

As an excel/libreoffice formula:

+----+-------------+--------------+
|..n.+.........2^n.+...LOG(2^n,2).|
+----+-------------+--------------+
|..1.+...........2.+...1..........|
|..4.+..........16.+...4..........|
|..8.+.........256.+...8..........|
|.16.+.......65536.+..16..........|
|.24.+....16777216.+..24..........|
|.30.+..1073741824.+..30..........|
|.31.+..2147483648.+..31..........|
|.32.+..4294967296.+..32..........|
+----+-------------+--------------+

So with A1=12345, =LOG(A1,2) gives 14 (2^14 = 16384)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文