define
- The values stored in the computer are all in the form of complement.
- The original, inverse, and complement of positive numbers are the same.
- The source code of a negative number is that the first bit of the absolute value of the negative number is changed to 1, the inverse is the first digit, the rest of the position is reversed, and the complement is the inverse +1.
public class Test{ public static void main(String[] args){ int a = 1000; Int b = -1000; int b = -1000; int b = -1000; /*-1000 binary source code: 10000000 00000000 00000011 11101000 inverse code: 1111111111 111111111100 00010111 complement: 11111111 11111111 11111100 00011000 this means that the number -1000 exists in the computer.Copy the code
Invert is invert for complement; It’s still a complement
- The inverse is for all bits, the inverse is to keep the first place unchanged;
public class Test{ public static void main(String[] args){ System.out.println(~1000); /** * 00000000 00000000 00000011 11101000 => 1000's complement * 1111111111 1111111100 00010111 => The inverse complement of 1000, that's the way it is in the computer, how do you get the external representation? We can understand the negative number of the original code to complement the inverse operation; * 11111111111111111111111100 00010110 => -1001 * */ system.out.println (~-1001); -1001 * */ system.out.println (~-1001); / * * * 1: Obtain the external representation of -1001 * 10000000 00000000 00000011 11101001 => source code of -1001 * 1111111111 11111100 00010110 => inverse code of -1001 * 11111111111111111111111100 00010111 => -1001's complement * 00000000 00000000 00000011 11101000 => Inverse complement is the final result 1000 */}}Copy the code
What happens to our calculations in computers?
- First of all, it is necessary to know that numerical values exist in the form of complement codes in the computer, so the calculated elements are also complement codes, and the calculated results are also complement codes.
public class OutClass { public static void main(String[] args) { int a = 2; int b = -6; System.out.println(a+b); /** * 00000000 00000000 00000000 00000010 +1 complement * 10000000 00000000 00000000 00000000 00000110-6 Original code * 1111111111 11111111 11111111111111111111111111111111111010-6 complement with complement to participate in the operation; 00000000 00000000 00000010 + 1111111111 11111111 11111111 11111010 -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 11111111, 11111111, 11111111, 11111100 / / get a result complement, because the first 1 is negative, 11111111 11111111 11111111 11111111 11111011 // subtract 1 from the complement to get the inverse; 10000000 00000000 00000000 00000100 // The first digit remains unchanged, and the other positions are reversed to obtain the original code -4 * */}}Copy the code