Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and the Yahoo Answers website is now in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.
Trending News
Why does the maximum size for a java int variable range from -2,147,483,648 to +2,147,483, 647?
I don't understand why the maximum number for the int variable is different if it's a negative number as opposed to a positive number. I also don't see how it could possibly reach -2,147,483,648.
I know that an int is made of 32 bits. The first bit is presumably used to determine whether the number is a positive or negative number (ie. 0 for a positive number, 1 for a negative number). This leaves 31 bits left for the maximum size of the number (whether it's positive or negative). If all the 31 digits are 1, then that would be the maximum size of the number. And if all the digits are 1, then you can use the formula 2^31 - 1 to find out that maximum number, which is 2,147,483,647. So therefore the maximum negative number should be -2,147,483,647, and the maximum positive number should be +2,147,483,647. So how can it reach -2,147,483,648 without overflowing?
6 Answers
- Jogger2425Lv 63 months agoFavorite Answer
Your reasoning is very good. But, you overlooked one thing: What you wrote would be true if there was a negative zero and a positive zero.
But, there is only one zero. So, the binary value for negative one occupies the place where negative zero would have been if there was a negative zero. Negative two occupies the place where negative one would have been if there was a negative zero.
For more information, do a web search for "two's complement." Compare that to "one's complement."
The following numbers are using two's compliment: Minus one is 0xFFFFFFFF. Minus 2 is 0xFFFFFFFE. Minus 3 is 0xFFFFFFFD, and so on until -2147473648 is 0x80000000. (The '0x' prefix indicates what follows is a hexadecimal number.)
Comment: Early computers could do operations on decimal numbers, using a number format known as Binary Coded Decimal or BCD. BCD does allow for the possibility of a negative zero or a positive zero.
Comment: A modern CPU might support BCD, but a lot of programming languages don't support it. So, hardware support for BCD may be dropped. Programming language support for BCD is one reason some old timer programmers and their managers still like COBOL.
Source(s): https://en.wikipedia.org/wiki/Two%27s_complement https://en.wikipedia.org/wiki/Ones%27_complement - ?Lv 73 months ago
0 is actually included in the positive set of numbers, since it has no minus. not sure about the rest, but i imagine there is a contingency for that final number, like a "min/max" thing.
- ?Lv 63 months ago
Tip: Try using words that might appear on the page you’re looking for. For example, "cake recipes" instead of "how to make a cake."
Source(s): https://iqosdubai.ae/ - Daniel HLv 53 months ago
the last bit is the sign bit. Take a look at this 8-bit example
125 01111101
126 01111110
127 01111111
what happens when you add 1 more
10000000
Is this negative zero? No. It gets evaluated as -128
- EddieJLv 73 months ago
One way that you can think of it is to give a value to the sign bit of
-2,147,483,648
When the remaining 31 bits are all zeros, then that zero value is added to the negative value of the sign bit to produce the largest negative value.
If the 31 bits are all zeroes EXCEPT for the rightmost digit (which is 1), that positive value of 1 is added to the largest negative value (the value of the sign bit) and the result will be -2,147,483,647.
If all 32 bits are 1, the rightmost 31 bits add up to 2,147,483,647, which will be added to the sign bit and the result will be -1.
- dewcoonsLv 73 months ago
Because on the positive side they have to include the number zero. There are still 2,147,483,648 "numbers on the positive side, But one is zero.