The original code, complement, anti-code is stored in a computer in binary mode.
Original code: is the simplest representation of the number of machines. Symbol bits represented by the most significant bit, '1' denotes a negative sign, '0' denotes a positive number. Other bits to store the absolute value of the binary number.
Anti-code: Positive anti-code or equal to the original code
Inverted negative is his original code in addition to the sign bit, Bitwise.
Complement: positive complement equal to his original code
negative anti-code complement equal to +1.
Among the computer is used to store complement.
E.g
Integer (int) 45 32 4 bytes Integer
Original code: 0,000,000,000,100,111
Anti-code: 1,111,111,111,011,000
Complement: 000 000000100111
Integer (int) -45
Original code: 1,000,000,000,100,111
Anti-code: 1,111,111,111,011,000
Complement: 1,111,111,111,011,001
Achieve an int in the Java language conversion as follows:
1 package ClassroomTest; 2 3 import java.util.Scanner; 4 5 public class Second { 6 7 8 9 @SuppressWarnings("resource") 10 public static void main(String[] args) { 11 int s[] =new int[32]; 12 int y[]=new int[32]; 13 int f[]=new int[32]; 14 int0 = I ; 15 int C; 16 Scanner SC = null ; . 17 System.out.println ( "Please enter an integer:" ); 18 is SC = new new Scanner (the System.in); . 19 int A = sc.nextInt () ; 20 is IF (A> = 0) { // integer Analyzing 21 is the while (A> =. 1) { // stored original code 22 is C = A 2% ; 23 is S [I] = C; 24 A = A / 2 ; 25 I ++ ; 26 is } 27 int j; 28 j=31; 29 for(i=0;i<s.length;i++) { 30 y[j]=s[i]; 31 j--; 32 } 33 System.out.println(); 34 System.out.println("原码:"); //输出原码 35 for(i=0;i<y.length;i++) { //符号位为0 36 System.out.print(y[i]); 37 if((i+1)%4==0) { 38 System.out.print(" "); 39 } 40 } 41 System.out.println(); 42 System.out.println("反码:"); //输出反码 全部取反 43 for(i=(y.length-1);i>=0;i--) { 44 if(y[i]==0) { 45 f[i]=1; 46 }else { 47 f[i]=0; 48 } 49 } 50 for(i=0;i<f.length;i++) { 51 System.out.print(f[i]); 52 is IF ((I +. 1) == 0. 4% ) { 53 is of System.out.print ( "" ); 54 is } 55 } 56 is System.out.println (); 57 is System.out.println ( "complement: "); // output the same complement of the original code 58 for (I = 0; I <y.length; I ++ ) { 59 of System.out.print (Y [I]); 60 IF ((I +. 1)% == 0. 4 ) { 61 is of System.out.print ( "" ); 62 is } 63 is } 64 } the else { // When the input integer is negative 65 A = - A; 66 the while (A> =. 1 ) { 67 C = A% 2 ; 68 S [I] = C; 69 A = A / 2 ; 70 I ++ ; 71 is } 72 int J; 73 is J = 31 is ; 74 for (I = 0; I <s.length; I ++ ) { 75 Y [J] = S [I]; 76 J, ; 77 } 78 Y [0] =. 1; 79 System.out.println (); 80 System.out.println ( "original code:"); // output primitive 81 for (I = 0; I <y.length; I ++) { // sign bit . 1 82 of System.out.print (Y [I]); 83 IF ((I +. 1) == 0. 4% ) { 84 of System.out.print ( "" ); 85 } 86 } 87 System.out.println (); 88 System.out.println ( "anti-code:"); // output of the inverse code 89 for (I = (. 1-y.length); I> = 0; i--) { // sign bit is not variants, other digital negated 90 if(y[i]==0) { 91 f[i]=1; 92 }else { 93 f[i]=0; 94 } 95 } 96 f[0]=1; 97 for(i=0;i<f.length;i++) { 98 System.out.print(f[i]); 99 if((i+1)%4==0) { 100 System.out.print(" "); 101 } 102 } 103 System.out.println(); 104 System.out.println ( "complement:"); // output complement 105 I = 31 is ; 106 F [31 is] = F [31 is] + 1'd ; 107 the while (I> = 0) { // convert in the inverted complement basis + 1'd 108 IF (F [I] == 2 ) { 109 F [I] = 0 ; 110 F [. 1-I] = F [. 1-I] + 1'd ; 111 } the else 112 { 113 F [I] =. 1 ; 114 } 115 i-- ; 1 16 } 117 118 for(i=0;i<f.length;i++) { 119 System.out.print(f[i]); 120 if((i+1)%4==0) { 121 System.out.print(" "); 122 } 123 } 124 125 } 126 127 } 128 }
Results are as follows:
Enter an integer:
-32
original code:
1,000,000,000,000,000 0,000,000,000,100,000
inverted:
1,111,111,111,111,111 1,111,111,111,011,111
complement:
1,111,111,111,111,111 1,111,111,111,100,000
Enter an integer:
32
original code:
0000 0000 0000 0000 0,000,000,000,100,000
inverted:
1,111,111,111,111,111 1,111,111,111,011,111
Complement:
0000 0000 0000 0000 0,000,000,000,100,000
reward:
Original code a positive integer, the inverted, no special place complement, no special memory; negative integers, but for the original code to be noted that the sign bit is 1, and to be converted to binary and then seek after a positive integer, for anti-code, attention other than the number of bits removed sign bit inversion, the inverted by adding one to the inverted basis (Do not forget to calculate in binary).