In a binary number, why there is the original code, anti-code complement

Since the binary number of only two kinds of 0 and 1, and therefore is much simpler than a decimal arithmetic operation circuit when the circuit built in the computer.

1, the original code

  In the system of the original code word length of n is, the leftmost bit represents the sign, in the computer, we produced two negative arbitrarily defined as the left most "1" represents a negative value, the leftmost binary number n is '0' a positive number. Such a range of values n-bit source word length of the system can be represented by (- (2 n. 1- -1) ~ (2 n. 1- -1))

  For example, -3 10 = 1011 2   , + 6 10 = 0110 2   .;

  At a time when computer that is a positive number can be negative numbers. But poses a problem, let us conduct a simple addition operation to see.

  610+(-310)=0110+10112 =(1)0001=1710 

  610 +310 =01102 +0011=10012 =(-110);

  Calculation error, so calculation can not be done directly to the original code. To solve this problem, there is a counter-code (1's complement) and complement (2's complement).

2, complement

  In the complement system, an n-bit word length N is a positive integer and complement per se is the leftmost 0, but an n-bit word of the complement of negative -N N and defined as follows:

  N * = 2 -N

  For example, when a = -5 -N 10 = 1101 2 , its complement N * equal to the corresponding positive number. 5 = N 10 participate in the following calculation, N * = 10000 2 -0101 2 = 1011 2 ;

   Another method of calculating the n-bit word length negative complement the leftmost symbol bits unchanged, the remaining bit is inverted, and then added to the rightmost 1.

  For example, -N = -5 10 = 1101 2 , N * = 1011 ;

 

 2.1 complement adder

  adding a binary number of n-bit symbol may be calculated directly complement system. The addition of the operation process is the same number as all positive, and carry the sign bit generated needs to be ignored . The following results n = 4 different situations arise.

  1, two positive numbers together, and less than 2 n-  -1.

  . 3 10 + 4 10 = 0011 2 +0100 2 = 0111 2 = 7 10 ; (correct result)

  2, two positive numbers together, and greater than 2 n-  -1.

. 5 10 + 6 10 = 0101 2 +0110 2 = 1011 = 11 10 > 2 . 3 ; (erroneous results)

  When two n-bit symbol number calculation result in the range (- (2 n. 1- -1) ~ (2 n. 1- -1)), the result is greater than this, it is called overflow.

  3, positive and negative adding a large absolute negative.

. 5 10 + (-. 6 10 ) = 0101 2 +1010 2 = 1111 2 = -1 10 <2 . 3 ; (correct result)

  4, the third case the same, but the absolute value is larger than the positive negative.

-5 10 + 6 10 = 1011 2 +0110 2 = (1) 0001 2 <2 . 3 ; (into the sign bit generated ignored, no overflow, the result is correct)

  5, two negative sum, and absolute value less than 2 n--. 1 .

  -3 2 + (-. 4 2 ) = 1101 2 +1100 2 = (1) 1001 2 = -7 10 ; (ignoring the sign bit of the carry, no overflow, the result is correct)

  6, two negative sum, and absolute value greater than 2 n--. 1 .

-5 10 + (-. 6 10 ) = 1011 2 +1010 2 = (1) 0101 2 = -11 10 <-2 . 3 ; (result overflows, an error results, expressed as -11 to signed totaling 5)

3, anti-code

  Anti-complement adder with a similar code, carry only the last discarded, but is not produced on the n-bit added again and the rightmost bit, which is called Carry cycle (end-around carry). Positive anti-complement code adding the previous two examples the same front. Given below where the rest of the anti-code adding (n = 4).

  3, by adding positive and negative, negative absolute value is large.

. 5 10 + (-. 6 10 ) = 0101 2 +1001 2 = 1110 2 = -1 10 ; (correct result)

  4, and (3) the same, but the absolute value of a positive number larger than negative.

-510+610=10102+01102=(1)00002;

0000 2 +0001 2 = 0001 2 = 1 10 (cycle carry, no overflow, the result is correct)

  5, two negative and the result is less than the absolute value of 2 n--. 1 .

-310+(-410)=11002+10112=(1)01112 ;

0111 2 +0001 2 = 1000 2 = -7 10 ; (around carry, no overflow, the result is correct)

  6, two negative numbers are added, the result is greater than the absolute value of 2 n--. 1 .

-510+(-610)=10102+10012=(1)01112;

0111 2 +0001 2 = 1000 2 = -7 10 ; (the result overflows, an error results)

  Notably, two negative addition result is positive, and therefore an error can be detected.

Guess you like

Origin www.cnblogs.com/Maxwill-Peng/p/11007093.html