十进制的 1x10^49 - 这是多少二进制位以及如何将其转换为二进制?
我遇到过一个网站,它在 URL 查询字符串中使用 50 位十进制整数 ID,这似乎有点过多。
最小的 50 位十进制数是 1.0 x 10^49
,也称为:
1000000000
0000000000
0000000000
0000000000
0000000000
- 二进制表示包含多少位?
- 考虑到无符号 32 位整数或 64 位整数的范围限制,您将如何将如此大的十进制数转换为二进制数?
我只是出于纯粹的程序员好奇心而问 - 这不是大学问题、工作问题或面试难题!
I've encountered a website that uses a 50-digit decimal integer ID in a URL query string, which seems a little excessive.
The smallest 50-digit decimal number is 1.0 x 10^49
, otherwise known as:
1000000000
0000000000
0000000000
0000000000
0000000000
- How many bits would the binary representation contain?
- How would you approach converting such a large decimal number to binary, taking into consideration the range limit of unsigned 32-bit-integer or 64-bit integers?
I ask out of pure programmer curiosity only - this is not a college question, work problem or interview puzzle!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
最小二进制表示(具有整数精度)可以通过取数字的对数(以 2 为底)来找到。 在这种情况下,最小二进制位数为 log(10^49) = 162.77。 我们需要一个整数,因此我们将其称为 163 位。
如果我必须表示该数字,并且浮点表示的精度不够,我只会使用一些 BigInteger 库。
The minimal binary representation(with integer precision) can be found by taking the log (base 2) of the number. In this case the minimal amount of binary bits would be log(10^49) = 162.77. We need a whole number so we will just call it 163 bits.
If I had to represent that number, and the precision in a floating point representation was insufficient, I would just use some BigInteger library.
49 * log(10) / log(2) = 162.774477,因此二进制表示将包含 163 位。
49 * log(
使用 bigint 类并应用标准算法将十进制转换为二进制.
49 * log(10) / log(2) = 162.774477, so the binary representation would contain 163 bits.
Use a bigint class and apply the standard algorithm for converting from decimal to binary.
由于每个十进制数字传达的信息与
lb 10
位相同,因此任何 50 位数字都适合ceil(lb(10)*50) = 167
位。具体来说,从十进制转换为二进制并不难,即使是手动转换也是如此。 只需除以二,然后将模数(如果最后一位数字是奇数则为 1,如果为偶数则为 0)放在二进制结果的末尾。 如果您在程序中需要如此大的数字,只需使用您平台的大整数实现,例如Java 中的
BigInteger
和Python 中的int
。 如果没有,请寻找数字库。哦,二进制的 10^49 是 163 位长:
Since every decimal digit conveys the same information as
lb 10
bits, any 50 digit number will fit intoceil(lb(10)*50) = 167
bits.Specifically, it's not that hard to convert from decimal to binary, even by hand. Just divide by two, and put the modulus(1 if the last digit was odd, 0 if even) at the end of your binary result. If you need such high numbers in a program, just use your platform's big integer implementation, e.g.
BigInteger
in Java and justint
in python. In the absence of that, look for a numerical library.Oh, and 10^49 in binary is 163 bit long:
人们可以使用合适的长整数操作库来转换这些数字。 如果不允许使用,则阅读源代码可以提供有关如何有效完成此类操作的有用知识。
关于解方程所需的位数:
2N = 1050
取两部分的 log2:
N = log 21050
现在将 log2 转换为 log10:
N = log2 1050 = log101050/log102 = 50 / log102
取 N 的下一个整数(ceil)——这就是所需的位数。
One could use a suitable longinteger manipulation library for converting such numbers. If using is not allowed reading the source can give useful knowledge of how such things are done efficiently.
Regarding the number of bits you just need to solve the equation:
2N = 1050
take a log2 of both parts:
N = log21050
Now convert log2 to log10:
N = log21050 = log101050/log102 = 50 / log102
Take the next integer (ceil) of N - that's the number of bits required.
1) 2^10 ~ 10^3 所以 10^48 ~ 2^160; 10^49 将是 164 位数量。
2) 使用 BigInteger 或 MPI 类(如果您的语言标准 API 库没有附带的话,可以找到很多此类类)。 高德纳 (Knuth) 有详细信息。
1) 2^10 ~ 10^3 so 10^48 ~ 2^160; 10^49 will be a 164 bit quantity.
2) Use a BigInteger or MPI class (of which there are plenty to be found if your language standard API library doesn't come with one). Knuth has the details.
我会使用一种为我处理大整数的高级语言。 示例 irb (Ruby) 会话:
I'd use a high-level language that handles big integers for me. Sample irb (Ruby) session:
存储数字 X 到底是什么意思?
我的直觉是面试官可能指的是第三个。 答案是 1 位。
What do you mean exactly by storing number X?
My gut feeling is the interviewer might have meant the third one. The answer would be 1 bit.
50 位十进制整数的范围是 10^49 到 10^50-1。 10^49 是 163 位,10^50-1 是 167 位。 如果您想要确切的位数,则需要直接取这些大数的对数,而不是仅仅计算“快捷方式”50*log10(2)。
作为替代方案,您可以使用任意精度十进制-二进制转换器将数字转换为二进制并计算位数(顺便说一句,我链接到的转换器为您计算位数)。
50 digit decimal integers range from 10^49 to 10^50-1. 10^49 is 163 bits, and 10^50-1 is 167 bits. If you want the EXACT number of bits, you need to take the logarithm of those big numbers directly, as opposed to just computing the "shortcut" 50*log10(2).
As an alternative, you can convert the number to binary using an arbitrary precision decimal-binary converter and count the bits (BTW, the converter I linked to counts the bits for you).