

Moderator: admin
Code: Select all
public class Test {
public static void main(String[] args) {
Integer x = 0;
System.out.println(x.MIN_VALUE);
System.out.println(x.MAX_VALUE);
}
}
Code: Select all
System.out.println(Integer.toBinaryString(x.MIN_VALUE));
System.out.println(Integer.toBinaryString(x.MAX_VALUE));
Code: Select all
int i =-45, System.out.println(-i);
HTH,Step 1 (complement the bits of 1): ///11111111 11111111 11111111 11111110///
Step 2 (add 1 to step 1): ///11111111 11111111 11111111 11111111///.
Code: Select all
// java program by javanaut :D
public class Foo {
static public void main(String[] args) {
Integer x = 0b11111111_11111111_11111111_11111111;
System.out.println(x);
Integer x1 = Integer.MAX_VALUE;
System.out.println(x1);
System.out.println(Integer.toBinaryString(x));
System.out.println(Integer.toBinaryString(x1));
}
}
Code: Select all
-1
2147483647
11111111111111111111111111111111
1111111111111111111111111111111
Code: Select all
Basically, because Integer.MAX_VALUE is actually only 2147483647, so -Integer.MIN_VALUE, which would be +2147483648, actually overflows the capacity of the internal binary representation of integers. Thus, the result "loops around" back to Integer.MIN_VALUE, or -2147483648.
Users browsing this forum: Bing [Bot] and 14 guests