Value of i for (i == -i && i != 0) to return true in Java -
i have following if
condition.
if (i == -i && != 0)
what value of i
return true
condition in java?
i unable think of such value of i
considering two's complement notation in java.
i love have algebraic proof of whatever answer condition has (in context java)?
the int
value works integer.min_value
.
it's because integers negated using two's complement way.
using
system.out.println(integer.tobinarystring(integer.min_value));
you see integer.min_value
is
10000000000000000000000000000000
taking negative value done first swapping 0
, 1
, gives
01111111111111111111111111111111
and adding 1
, gives
10000000000000000000000000000000
as can see in link gave, wikipedia mentions problem negative numbers , specifies it's sole exception :
the negative number in two's complement called "the weird number," because exception.
of course have same phenomenon long.min_value
if store in long
variable.
note this due choices made regarding binary storage of ints in java. (bad) solution example have been negate changing significant bit , letting other bits unchanged, have avoided problem min_value have made 2 different 0
values , complicated binary arithmetic (how have incremented example ?).
Comments
Post a Comment