@Test
public void testName() throws Exception {
/*
* 负整数的二进制是怎么存的?
* 在计算机中,负数以其正值的补码形式表达。
* 什么叫补码呢?这得从原码,反码说起。
* -----------------------------------------------
* 5的原码(概念:一个整数,按照绝对值大小转换成的二进制数,称为原码。)
* 00000000 00000000 00000000 00000101 5
*
* 5的反码(概念:将二进制数按位取反,所得的新二进制数称为原二进制数的反码。取反操作指:原为1,得0;原为0,得1。(1变0; 0变1)。反码是相互的。)
* 11111111 11111111 11111111 11111010
*
* 5的补码(概念:反码加1称为补码。)
* 11111111 11111111 11111111 11111011 -5
*/
System.out.println(Integer.toBinaryString(5)); // 5的原码:101
System.out.println(Integer.toBinaryString(~5)); // 5的反码:11111111111111111111111111111010
System.out.println(Integer.toBinaryString(~5 + 1)); // 5的补码:11111111111111111111111111111011
System.out.println(Integer.toBinaryString(-5)); // -5的二进制,和5的补码相同:11111111111111111111111111111011
}