补码:
补码的规律
* 1. int最大值: 一个0, 31个1
* 16进制: 0x7fffffff
* 2. int最小值:
* 16进制: 0x80000000
* 3. -1 : 在内存中是32个1
* 16进制: 0xffffffff
int max = Integer.MAX_VALUE;
System.out.println(max);
System.out.println(
Integer.toBinaryString(max));
补码的互补对称现象:
int n = 8;
int m = ~n+1;
System.out.println(m);
System.out.println(Integer.toBinaryString(n));
System.out.println(Integer.toBinaryString(~n));
System.out.println(Integer.toBinaryString(~n+1));
//经典面试题目:
System.out.println(~100+1);
//如上代码输出结果:
//A.99 B.-99 C.100 D.-100 E.101
//答案: D
//经典面试题目:
System.out.println(~100);
//如上代码输出结果:
//A.99 B.-99 C.100 D.-100 E.-101
//答案: E
//经典面试题目:
System.out.println(~-100);
//如上代码输出结果:
//A.99 B.-99 C.100 D.-100 E.-101
//答案: A
网友评论