简体   繁体   English

为什么符号位不影响Integer.MAX_VALUE但影响MIN值?

[英]Why does the sign bit not affect the Integer.MAX_VALUE but affects the MIN Value?

System.out.println(Integer.MAX_VALUE);
System.out.println(Integer.MIN_VALUE);

Prints: 打印:

2147483647
-2147483648

Why is the max value 2^31 -1 (the sign bit is 0 and does not add to the value of the number) and yet the min value is just -2^31 (the sign bit is 1 and does add to the value then??). 为什么最大值为2 ^ 31 -1(符号位为0且未加到数字的值),而最小值为-2 ^ 31(符号位为1且已加到值)然后??)。

Think about it this way: you have as many binary patterns with the sign bit set to 1 as the number of binary patterns with the sign bit set to 0 . 这样考虑:与符号位设置为0的二进制模式数一样,您拥有的符号位设置为1的二进制模式数也是如此。 However, you also need to represent zero, which is neither positive nor negative. 但是,您还需要表示零,既不是正数也不是负数。 Since zero is represented as a pattern of all zeros, it deducts from the set of positive numbers representable with the given number of bits, so the count of representable negative numbers is going to be greater by one. 由于零表示为全零的模式,因此它从给定位数的正数集合中扣除,因此可负数的计数将增加一个。

The sign bit does not add to the value. 符号位不会加到该值上。 You can represent 2^32 different values with 32-bits. 您可以用32位表示2 ^ 32个不同的值。 Hovewer, one of those values is 0; Hovewer,其中一个值是0; so there are 2^31 negative values, 2^31 - 1 positive values and 0, which all add up to 2^32 different values. 因此有2 ^ 31个负值,2 ^ 31-1个正值和0,它们总共加起来有2 ^ 32个不同的值。 Since the sign bit of 0 is also 0, it is only natural that the number of positive values are one less then the number of negative values. 由于符号位0也为0,因此很自然地,正值的数量要比负值的数量少一。

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

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