十进制的 1x10^49 - 这是多少二进制位以及如何将其转换为二进制?

发布于 2024-07-27 17:51:39 字数 329 浏览 5 评论 0原文

我遇到过一个网站,它在 URL 查询字符串中使用 50 位十进制整数 ID,这似乎有点过多。

最小的 50 位十进制数是 1.0 x 10^49,也称为:

1000000000
0000000000
0000000000
0000000000
0000000000
  1. 二进制表示包含多少位?
  2. 考虑到无符号 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
  1. How many bits would the binary representation contain?
  2. 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 技术交流群。

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

发布评论

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

评论(8

梦明 2024-08-03 17:51:39

最小二进制表示(具有整数精度)可以通过取数字的对数(以 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.

溺孤伤于心 2024-08-03 17:51:39
  1. 49 * log(10) / log(2) = 162.774477,因此二进制表示将包含 163 位。

    49 * log(

  2. 使用 bigint 类并应用标准算法将十进制转换为二进制.

  1. 49 * log(10) / log(2) = 162.774477, so the binary representation would contain 163 bits.

  2. Use a bigint class and apply the standard algorithm for converting from decimal to binary.

救星 2024-08-03 17:51:39

由于每个十进制数字传达的信息与 lb 10 位相同,因此任何 50 位数字都适合 ceil(lb(10)*50) = 167 位。

具体来说,从十进制转换为二进制并不难,即使是手动转换也是如此。 只需除以二,然后将模数(如果最后一位数字是奇数则为 1,如果为偶数则为 0)放在二进制结果的末尾。 如果您在程序中需要如此大的数字,只需使用您平台的大整数实现,例如Java 中的BigInteger 和Python 中的int。 如果没有,请寻找数字库。

哦,二进制的 10^49 是 163 位长:

110
1101 0111 1001 1111 1000 0010 0011 0010
1000 1110 1010 0011 1101 1010 0110 0001
1110 0000 0110 0110 1110 1011 1011 0010
1111 1000 1000 1010 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000

Since every decimal digit conveys the same information as lb 10 bits, any 50 digit number will fit into ceil(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 just int in python. In the absence of that, look for a numerical library.

Oh, and 10^49 in binary is 163 bit long:

110
1101 0111 1001 1111 1000 0010 0011 0010
1000 1110 1010 0011 1101 1010 0110 0001
1110 0000 0110 0110 1110 1011 1011 0010
1111 1000 1000 1010 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000
乱了心跳 2024-08-03 17:51:39

人们可以使用合适的长整数操作库来转换这些数字。 如果不允许使用,则阅读源代码可以提供有关如何有效完成此类操作的有用知识。

关于解方程所需的位数:

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.

时光暖心i 2024-08-03 17:51:39

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.

眼波传意 2024-08-03 17:51:39

我会使用一种为我处理大整数的高级语言。 示例 irb (Ruby) 会话:

>> (10**49).to_s
=> "10000000000000000000000000000000000000000000000000"
>> (10**49).to_s(2)
=> "1101101011110011111100000100011001010001110101000111101101001100001111000000110011011101011101100101111100010001010000000000000000000000000000000000000000000000000"
>> (10**49).to_s(2).size
=> 163

I'd use a high-level language that handles big integers for me. Sample irb (Ruby) session:

>> (10**49).to_s
=> "10000000000000000000000000000000000000000000000000"
>> (10**49).to_s(2)
=> "1101101011110011111100000100011001010001110101000111101101001100001111000000110011011101011101100101111100010001010000000000000000000000000000000000000000000000000"
>> (10**49).to_s(2).size
=> 163
往事风中埋 2024-08-03 17:51:39

存储数字 X 到底是什么意思?

  1. 你的意思是存储0到X之间的所有数字吗?
  2. 或者 -X 和 X 之间的所有数字?
  3. 或者只存储信息:null / X。

我的直觉是面试官可能指的是第三个。 答案是 1 位。

What do you mean exactly by storing number X?

  1. Do you mean storing all the numbers between 0 and X?
  2. Or all the numbers between -X and X?
  3. Or only storing the information: null / X.

My gut feeling is the interviewer might have meant the third one. The answer would be 1 bit.

花开柳相依 2024-08-03 17:51:39

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

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