4 Replies Latest reply: Feb 8, 2013 9:51 AM by rp0428

# Subtraction of a positive integer of a negative integer results a positive?

Hi

I have the next problem and I haven't found the reason...
I have the next Java code:

int a = -2147483648;
int b = 2147483647;

System.out.println(a - b);

The output of this lines is "1", but I don't understand why.

Thanks
• ###### 1. Re: Subtraction of a positive integer of a negative integer results a positive?
It has to do with overflow. The primitives wrap around after the Integer.MAX_VALUE and Integer.MIN_VALUE; which are the two values you have there. Have a look at this link, as it gives a pretty good answer I think.

http://stackoverflow.com/questions/5131131/what-happens-when-you-increment-an-integer-beyond-its-max-value
• ###### 2. Re: Subtraction of a positive integer of a negative integer results a positive?
>
I have the next problem and I haven't found the reason...
I have the next Java code:

int a = -2147483648;
int b = 2147483647;

System.out.println(a - b);

The output of this lines is "1", but I don't understand why.
>
First subtract the numbers in decimal:
``-2147483648 - 2147483647 = -4294967295``
If you convert that result to hexadecimal as a 'long' you get
``-4294967295 = FFFFFFFF00000001x``
But Java uses 'int' by default and an 'int' is 32 bits (4 bytes). The low-order 4 bytes of that result are '00000001x' which is '1'.

If you do the math yourself using only 32 bits and use the HEX representation for those numbers it will make more sense.
``````int a = -2147483648 = 80000000x
int b = 2147483647  = 7FFFFFFFx``````
Just subtract the numbers like you normally would starting at the right.

1. Substract 'F' from '0' - oops! You need to borrow '1' from the digit to the left which is '0'. You wind up borrowing one from the '8' and getting 10x. Subtract 0Fx from 10x and you get '1' for the right-most digit.

2. Now the second digit from the right is F on the bottom and F on the top (we borrowed one) - subtract and you get 0

3. The 80000000x became 7FFFFFF + 10x when we first borrowed and the '7FFFFFF' is the same as the bottom so all other digits are 0.

If you look at two similar numbers in decimal and do the subtraction it may be easier to see:
``````a = 5000
b = 4999 (in decimal '9' is the largest 'digit', in hexadecimal 'F' is the largest digit)

What do you get if you subtract those two numbers? You get '1' because when you try to subtract the rightmost '9' from the '0' you have to borrows one from the digit to the left which means borrowing one from the '5' essentially giving the top number as '499' + '10'. Subtract 9 from the 10 giving 1. Then the top has '499' remaining and so does the bottom so the rest are zeroes: 0001.``````
• ###### 3. Re: Subtraction of a positive integer of a negative integer results a positive?
Thanks for your very clear explanation...

My main issue was that I wasn't sure of the Java process for this operation, and I thought that maybe the result
-4294967295 = FFFFFFFF00000001
could throw an exception.

Regards...
• ###### 4. Re: Subtraction of a positive integer of a negative integer results a positive?
>
My main issue was that I wasn't sure of the Java process for this operation, and I thought that maybe the result
-4294967295 = FFFFFFFF00000001
could throw an exception.
>
The answer to that concern is in the Java Language Spec.
http://docs.oracle.com/javase/specs/jls/se7/jls7.pdf

See sections 4.2.1 for the value ranges (you will see your MAX and MIN 'int' values there
>
4.2.1 Integral Types and Values
The values of the integral types are integers in the following ranges:
• For byte, from -128 to 127, inclusive
• For short, from -32768 to 32767, inclusive
• For int, from -2147483648 to 2147483647, inclusive
• For long, from -9223372036854775808 to 9223372036854775807, inclusive
• For char, from '\u0000' to '\uffff' inclusive, that is, from 0 to 65535
4.2.2 Integer Operations
The Java programming language provides a number of operators that act on integral
values:
• The comparison operators, which result in a value of type boolean:
◆ The numerical comparison operators <, <=, >, and >= (§15.20.1)
◆ The numerical equality operators == and != (§15.21.1)
• The numerical operators, which result in a value of type int or long:
◆ The unary plus and minus operators + and - (§15.15.3, §15.15.4)
◆ The multiplicative operators *, /, and % (§15.17)
◆ The additive operators + and - (§15.18)
◆ The increment operator ++, both prefix (§15.15.1) and postfix (§15.14.2)
◆ The decrement operator --, both prefix (§15.15.2) and postfix (§15.14.3)
◆ The signed and unsigned shift operators <<, >>, and >>> (§15.19)
◆ The bitwise complement operator ~ (§15.15.5)
◆ The integer bitwise operators &, ^, and | (§15.22.1)
• The conditional operator ? : (§15.25)
• The cast operator (§15.16), which can convert from an integral value to a value
of any specified numeric type
• The string concatenation operator + (§15.18.1), which, when given a String
operand and an integral operand, will convert the integral operand to a String
representing its value in decimal form, and then produce a newly created String
that is the concatenation of the two strings
>
These are the two statements that apply directly to your question and concern
>
Otherwise, the operation is carried out using 32-bit precision, and the result of the
numerical operator is of type int. If either operand is not an int, it is first widened
to type int by numeric promotion.
. . .
The integer operators do not indicate overflow or underflow in any way.
>
Yours are 'integer operators' operating on 'int' values.