简体   繁体   English

需要对Java的32位整数表示系统进行说明吗?

[英]clarification needed on Java's 32-bit integer representation system?

I need some clarifications regarding the binary representation of decimal in Java (or any other language for that matter). 我需要一些有关Java(或其他任何语言)的二进制表示形式的说明。

pardon, if this is too basic, but I need to understand it thoroughly. 请原谅,如果这太基本了,但是我需要彻底了解它。

x = 31 is represented in Java 32 bit as:

x — > 00000000 00000000 00000000 00011111  // 

the binary to decimal conversion is achieved by:
                               2^4+2^3+2^2+2^1+2^0=31

Now, if you consider all the bits turned on, except the signed bit (most significant bit) , we get 现在,如果您考虑所有打开的位,除了有signed bit (most significant bit) ,我们将得到

y -> 01111111 11111111 11111111 11111111

and binary to decimal conversion by summing powers of 2 will be:
        2^31+2^30………+2^3+2^2+2^1+2^0=4294967295.

however, if you do: 但是,如果您这样做:

System.out.println(Integer.parseInt("1111111111111111111111111111111",2));
you get: 2147483647 which is 2^31-1

so it means, the when 31 bits are turned on, I do not get the additive sum of the powers of 2. why is this? 所以这意味着,当打开31 bits时,我没有得到2的幂的加和。这是为什么?

may be I did not understand something, if some one could clarify that will be very helpful. 可能是我听不懂某件事,如果有人可以澄清这一点会很有帮助。

In case this helps: All the two raise powers till 31 are in this list: 如果这有帮助:直到31的所有这两个提升能力都在此列表中:

[1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536, 131072, 262144, 524288, 1048576, 2097152, 4194304, 8388608, 16777216, 33554432, 67108864, 134217728, 268435456, 536870912, 1073741824, 2147483648]

EDIT: I corrected the y -representation, now it has 32 bits, but if you calculate the 31 bits that are turned on with summing the powers, you would get 4294967295. I have one liner in python here 编辑:我更正了y表示形式,现在它具有32位,但是如果您计算与幂加在一起就打开的31位,您将得到4294967295。我在python中有一个内衬

>>> reduce(lambda x,y: x+y, [pow(2,i) for i in range(32)])
4294967295

The problem is that here: 问题是在这里:

y -> 011111111 11111111 11111111 11111111

and binary to decimal conversion by summing powers of 2 will be:
        2^31+2^30………+2^3+2^2+2^1+2^0=4294967295.

you've put one too many 1s. 您输入了太多1。 As Jon Skeet mentioned in the comments, you should have only 31 1s, not 32. Thus, the sum should start at 2^30, not 2^31. 正如乔恩·斯基特(Jon Skeet)在评论中提到的那样,您应该只有31个1,而不是32。因此,总和应从2 ^ 30开始,而不是2 ^ 31。

( Update : Well, you've updated that bit to have the correct number of 1s. My statement about the sum though, still stands. It should start at 2 30 , not 2 31 . ) 更新 :嗯,您已经将该位更新为正确的1s数。尽管如此,我的总和声明仍然成立。 它应从2 30开始,而不是2 31。

When you did this part: 当您执行此部分时:

System.out.println(Integer.parseInt("1111111111111111111111111111111",2));
you get: 2147483647 which is 2^31-1

you have the number of 1s correct (31). 您有正确的1位数(31)。

so it means, the when 31 bits are turned on, I do not get the additive sum of the powers of 2. 所以这意味着,当打开31位时,我没有得到2的幂的加和。

Yes you do - you get 2 0 + 2 1 + 2 2 + 2 3 + ... + 2 30 ... which is 2 31 - 1, exactly as you're seeing. 是的,您会做的-您得到2 0 + 2 1 + 2 2 + 2 3 + ... + 2 30 ...就是2 31-1 ,正如您所看到的。

You're not adding 2 31 because that would be represented by a 32nd 1 , at which point you'd be beyond the limit of what an int can represent in Java. 没有添加2 31,因为它将由32nd 1表示,这时您将超出int可以在Java中表示的范围。

It's probably easier to consider smaller numbers. 考虑较小的数字可能会更容易。 Suppose you have 5 bits - your example earlier on: 假设您有5位-前面的示例:

Integer.parseInt("11111")

That would print out 31, exactly as you said before... because 31 is 2 5 - 1. 就像您之前所说的那样,它将打印出31,因为31是2 5-1

So for n bits all set to 1, you get 2 n - 1... which is correct for your example with 31 bits. 因此,对于全部设置为1的n位,您将得到2 n -1 ...对于您的示例(31位)是正确的。 There's nothing inconsistent here. 这里没有任何矛盾之处。

y -> 01111111 11111111 11111111 11111111
----------------------------------------
     31       23       15       7

The number is the index of the most significant bit in each byte. 该数字是每个字节中最高有效位的索引。

So 0x2^31 + 1x2^30 + 1x2^29 + ... + 1x2^0 = 2147483647 所以0x2^31 + 1x2^30 + 1x2^29 + ... + 1x2^0 = 2147483647

You were simply going one bit too far. 你只是太过分了。

You can actually test this 你可以实际测试一下

int a = 0b01111111_11111111_11111111_11111111;
System.out.println(a);

prints 版画

2147483647

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM