Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Value of i for (i == -i && i != 0) to return true in Java

Tags:

java

I have the following if condition.

if (i == -i && i != 0) 

What value of i will return true for this condition in Java?

I am unable to think of any such value of i considering two's complement notation in Java.

I would also love to have algebraic proof of whatever answer this condition has (in context with Java)?

like image 694
Sunny Avatar asked Jul 22 '13 06:07

Sunny


People also ask

Which value is Falsy JavaScript?

Description. A falsy value is something which evaluates to FALSE, for instance when checking a variable. There are only six falsey values in JavaScript: undefined , null , NaN , 0 , "" (empty string), and false of course.

Is Falsy or truthy?

In JavaScript, a truthy value is a value that is considered true when encountered in a Boolean context. All values are truthy unless they are defined as falsy. That is, all values are truthy except false , 0 , -0 , 0n , "" , null , undefined , and NaN .

What is equal value and equal type?

Equal value and equal type operator is an comparison operator which is used to check the whether two operands are having same value and same type.


2 Answers

The only int value for which it works is Integer.MIN_VALUE.

It's because integers are negated using the two's complement way.

Using

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

you see that Integer.MIN_VALUE is

10000000000000000000000000000000 

Taking the negative value is done by first swapping 0 and 1, which gives

01111111111111111111111111111111 

and by adding 1, which gives

10000000000000000000000000000000 

As you can see in the link I gave, Wikipedia mentions the problem with the most negative numbers and specifies it's the sole exception :

The most negative number in two's complement is sometimes called "the weird number," because it is the only exception.

Of course you have the same phenomenon for Long.Min_Value if you store it in a long variable.

Note that this is only due to choices that were made regarding the binary storage of ints in Java. Another (bad) solution could for example have been to negate by simply changing the most significant bit and letting the other bits unchanged, this would have avoided this problem with MIN_VALUE but would have made 2 different 0 values and complicated binary arithmetic (how would you have incremented for example ?).

like image 113
Denys Séguret Avatar answered Sep 28 '22 04:09

Denys Séguret


The value you are looking for is Integer.MIN_VALUE.


I would also love to have algebraic proof of whatever answer this condition has(in context with java)?

That's off-topic for Stack Exchange. But you could do it starting from the definition of Java integers (JLS 4.2)

"The integral types are byte, short, int, and long, whose values are 8-bit, 16-bit, 32-bit and 64-bit signed two's-complement integers ..."

and

"The values of the integral types are integers in the following ranges ... For int, from -2147483648 to 2147483647, inclusive"

and the definition of the Java unary '-' operator (JLS 15.15.4):

"For integer values, negation is the same as subtraction from zero. The Java programming language uses two's-complement representation for integers, and the range of two's-complement values is not symmetric, so negation of the maximum negative int or long results in that same maximum negative number. Overflow occurs in this case, but no exception is thrown. For all integer values x, -x equals (~x)+1."

like image 25
Stephen C Avatar answered Sep 28 '22 03:09

Stephen C