문제

I just started using Bluej to learn more about how computers store integers. I have a small program that I put into Bluej that sets the value of an integer called x to MAX_VALUE - 3 then adds 1 to x six times, printing out a new value each time. One addition is incorrect, although I need help understanding which value I received in incorrect and why the results I got are "strange".

Please keep in mind I am VERY naive to the language for computers and am literally reading from a book about storing integers. The book I have is Computer Science 11th edition by J. Glenn Brookshear. Here is the program I put into BlueJ:

 public class Add 
 { 
  public Add() 
  { 
  int i, x; 

 x = java.lang.Integer.MAX_VALUE - 3; 
 i = 0; 
 while (i < 6) { 
 x = x + 1; 
 i = i + 1; 
 System.out.print(x + "\n"); 
    } 
  } 
 } 

The values I receive are:

2147483645

2147483646

2147483647

-2147483648

-2147483647

-2147483646

My teacher says there is a problem with any integer math but I do not know what he means. I would just really like to understand why this happens. I might also note that these numbers are very much larger than 1 and I do not know why. Thank you all in advance for any responses!

도움이 되었습니까?

해결책

Integers that you store with the int data type are only allocated a limited amount of space in your computer's memory. It's not possible to store every possible integer in this amount of space. So your computer will deal correctly with integers between -2147483648 and 2147483647, because those are enough for most purposes. If you want to store numbers that are outside this range, you need to use a different data type from int. For example, there's long (which has a much bigger range) and BigInteger (which is really limited only by the amount of space allocated to Java itself).

When you add 1 to the largest possible int, the "correct" answer can't fit in an int variable. This is a bit like having an abacus with only one line of beads (which can represent numbers from 0 to 9), and trying to work out 9 + 1. Your computer will roll the number over to the smallest possible int instead. So when you work with int values, the effect is that 2147483647 + 1 = -2147483648, even though mathematically this makes no sense.

다른 팁

There is a limit value for an integer in Java in this case max_value.... for example when you try to surpass that value it becomes the oposite (-2,147,483,648 min_value). Like completing the circle and go back to the beggining. So there in no higher value than 2,147,483,647 ...so when you add 1 to that value you get the min_value instead...think of it like a snake eating his own tail ;)

If your Windows calculator has a Programmer View, switch to it, click Dword, enter 2147483645, add 1 six times, and watch the bits.

An integer in Java is 32-bits and signed. This means there is one sign bit and 31 bits for the value.

Integer.MAX_VALUE = 2147483647 (base 10)
                  = 0111 1111 1111 1111 1111 1111 1111 1111 (base 2)

Adding 1 yields
2147483647 + 1 = 2147483648
               = 1000 0000 0000 0000 0000 0000 0000 0000

Counting up, this is what you'd expect if you weren't a computer (or your number wasn't bounded by representation space). But with the int data type, we only get 32 bits and the "first" (not technically correct, but will aid in understanding) tells you whether or not the value is negative.

Now when Java translates this value to base 10 and because this is the signed integer data type...

2147483647 + 1 = 2147483648
               = 1000 0000 0000 0000 0000 0000 0000 0000

We read the first bit as 1 so this is a negative number and we need to take its 
twos-complement to calculate the value. 

               = 1000 0000 0000 0000 0000 0000 0000 0000
negated        = 0111 1111 1111 1111 1111 1111 1111 1111
+ 1            = 1000 0000 0000 0000 0000 0000 0000 0000
               = 2147483648 (base 10)

so when we display this value, it's the negative value of the two's complement, 

 = -2147483648 

The problem with "integer math" your teacher mentions is that, when your data type (Java's int, in this case) is bounded by size, your operations must make sense within its range.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top