The original code, the complement indicates

Source: Internet
Author: User

There are 3 representations of the fixed-point number in the computer: original code, inverse code, and complement

The so-called original code is described in the previous binary fixed-point notation, that is, the highest bit is the sign bit, "0" is positive, "1" is negative, the remaining bits represent the size of the value.

The inverse code notation stipulates that the inverse code of a positive number is the same as its original code, and the inverse of a negative number is a bitwise negation of its original code, except for the sign bit.

The complement notation stipulates that the complement of positive numbers is the same as the original code, and that the complement of negative numbers is added to the minus 1 of the inverse code.

The inverse code and complement of positive numbers are the same as the original code.

The inverse of a negative number is the negation of the original code except the sign bit.
[-3] anti-=[10000011] anti-=11111100
The complement of a negative number is to add 1 to the bottom after you have reversed the original code except for the sign bit.
[-3] complement =[10000011] complement =11111101
A number and its complement are reversible.

Have the original code on it, why do you need anti-code and complement?

Anti-code is used to complement, both the original code and the complement is used in the basic operation of the CPU, such as the data type is short:
Calculate 5-2, and because the CPU actually does not implement the subtraction circuit (note: The computer hardware structure only adder, so most of the operations must eventually be converted to addition, the original code does not have the means to do subtraction, and in our use of the Assembly, C and other high-level language used in the original code, The original code is converted to a complement at the bottom of the computer). The original code calculation is 5+ (-2)

0101

+1010

-------

1111


=-7? Obviously wrong.
So either positive or negative, use complement to indicate (positive and complement is the same), 2 of the complement is 1110, and then with 5 complement + 2 complement
0101
+ 1110
------
0011

=3, right
So theoretically (and just theoretically) we just let the meiosis pass through a reverse circuit, then through a +1 circuit, and then through the addition circuit can be achieved subtraction.

So the complement is designed to:
⑴ allows the symbolic potential energy to participate in the operation with the valid Values section, simplifying the operation rules.
⑵ the subtraction operation into addition, further simplifying the circuit design of the computing device in the computer

The original code and the inverse code at the time of the representation of the non-uniqueness, such as when the zero, the original code there are two kinds of notation:

[-0] Hara =10000000

[+0] Original =00000000

There are two representations of the inverse code:

[+0] Anti-=00000000

[-0] anti-=11111111

And the complement is only one [-0] complement = 00000000.

Online copy some in-depth introduction of its concept material

1, the original code, the inverse code and the complement expression method

(1) The original code: directly before the value of a symbol to add a bit of notation.

Example: Symbol bit value bit

[+7] Original = 0 0000111 B

[-7] original = 1 0000111 B

Attention:
A. The original code of number 0 has two forms:

[+0] Original =00000000b [-0] Original =10000000b

B. 8-bit binary source representation range: ( -127~-0 +0~127) a total of 256.

(2) Anti-code:

Positive number: The inverse of the positive number is the same as the original code.

Negative number: The inverse of the negative, the sign bit is "1", the numeric part is reversed by a bit.

Example: Symbol bit value bit

[+7] anti = 0 0000111 B

[-7] anti = 1 1111000 B

Note: A. There are two types of anti-code for number 0, namely

[+0] Anti-=00000000b

[-0] anti-=11111111b

B. 8-bit binary anti-code representation range: ( -127~-0 +0~127) a total of 256.

3) How to express the complement

1) The concept of a module: a unit of measurement called modulus or modulus. For example, the clock is counted as a 12-based loop, which is modulo 12. On the clock, the hour hand is added (positive dial) to the integer digits of 12 or minus (reversed) 12 of the integer digits, the position of the hour hand is constant. 14 O'Clock after the Die 12, become (PM) 2 o'clock (14=14-12=2). From 0 to 10, minus 10 hours, it can also be seen from 0 points clockwise Dial 2 (plus 2 hours), that is, 2 points (0-10=-10=-10+12=2). Therefore, under the premise of modulo 12, 10 can be mapped to +2. Thus, for a circulatory system with a modulus of 12, the effect of adding 2 and minus 10 is the same; therefore, in 12-mode systems, all operations minus 10 can be replaced by the addition of two, which translates the subtraction problem into an additive problem (note: The computer's hardware structure is only adder, So most of the operations must eventually be converted to addition. 10 and 2 are complementary to modulo 12.

Similarly, the computer's operating parts and registers have a certain word length limit (assuming the word length is 8), so its operation is also a modulo operation. When the counter is full of 8 bits, which is 256 digits, an overflow is generated and counted from the beginning. The amount of overflow generated is the module of the counter, obviously, the 8-bit binary number, its modulus is 28=256. In the calculation, two complementary numbers are called "complement".

2) Complement expression: Positive number: The complement of the positive number is the same as the original code.

    For a positive number of machines, the [x] Original =[x] complements the

For a negative number of machines, there is [x] original =[[x] complement]

Negative numbers: The complement of a negative number is the sign bit is "1", the value of the part is reversed after the first (lowest bit) plus 1. That is, "anti-code +1."

Example: Symbol bit value bit

[+7] complement = 0 0000111 B

[-7] complement = 1 1111001 B

The complement is an important form of coding in a microcomputer, please note:

A. After using the complement, the subtraction operation can be easily converted into addition operation, and the operation process is simplified. A positive complement is the true value of the number it represents, and the complement of a negative number is not the truth of what it represents. The results are still in complement with the complement operation.

B. Unlike the original code, the inverse code, the value of 0 of the complement only one, that is, [0] complement =00000000b.

C. If the word length is 8 digits, the complement is replaced by (-128) (-0), so the range of complement is: ( -128~0~127) a total of 256; When doing the complement operation, it should be noted that the result should not exceed the range of the complement can be expressed.

Note: (-128) does not correspond to the original code and the inverse code, (-128) = (10000000)

The use of complement not only fixes 0 of the symbols and two coding problems, but also can represent a minimum number. This is why the 8-bit binary, which uses the original code or the inverse code to represent a range of [-127, +127], and the use of the complement expressed in the range of [-128, 127].

because the machine uses the complement , for the 32-bit int type that is commonly used in programming, you can indicate that the range is: [-231, 231-1] because the first bit represents the sign bit. You can save a minimum value when you use the complement notation.

Four original code, anti-code, complement and further deep

The computer skillfully participates in the operation of the sign bit and turns the subtraction into addition, what is the mathematical principle behind it?

Think of a clock as a 1-bit 12-digit binary number. If the current time is 6 points, I would like to set the time to 4 points, how to do it? We can:

1. Dial back 2 hours: 6-2 = 4

2.10 hours Forward: (6 +) MoD 12 = 4

3. Forward 10+12=22 Hours: (6+22) mod 12 =4

The MoD in the 2,3 method refers to the modulo operation, and the remainder of MoD 12 =4 16 divided by 12 is 4.

So the result of clock back (subtraction) can be replaced by forward dialing (addition)!

The focus now falls on how to use a positive number instead of a negative number. In the example above we can feel some clues and find some rules. But maths is rigorous. Not by feeling.

First, a related concept in mathematics is introduced: congruence

Concept of congruence

Two integers a, b, if they are divided by the remainder of the integer m, it is said that a, b for modulo m congruence

Remember as A≡b (mod m)

Read A and b about modulo m congruence.

To illustrate:

4 MoD 12 = 4

MoD 12 = 4

MoD 12 = 4

So 4, 16, 28 about modulo 12 congruence.

Negative number modulo

A positive number for the MoD operation is simple. But what about negative numbers?

Here is a mathematical definition of the MoD operation:

Above is, "remove the bounds" symbol can not find how to input (pasted in Word after the garbled). The following are the "remove bounds" symbols replaced with "L" and "J":

X mod y = x-y L x/y J

The above formula means:

x mod y equals x minus y by the lower bound of the quotient of X and Y.

Take 3 mod 2 for example:

-3 MoD 2

= -3-2XL-3/2 J

= -3-2xl-1.5j

= -3-2x (-2)

=-3 + 4 = 1

So:

( -2) MoD 12 = 12-2=10

( -4) MoD 12 = 12-4 = 8

( -5) MoD 12 = 12-5 = 7

Start proving

Back to the question of the clock:

Callback 2 hours = 10 hours before dialing

Callback 4 hours = 8 hours before dialing

Callback 5 hours = 7 hours before dialing

Attention, the law found here!

Combined with the concept of congruence as learned above. In fact:

( -2) MoD 12 = 10

MoD 12 = 10

-2 and 10 are the same.

( -4) MoD 12 = 8

8 MoD 12 = 8

-4 and 8 are the same.

The distance is getting nearer and closer to success. To achieve a positive substitution of negative numbers, only two theorems of the same remainder are required:

Reflexivity:

A≡a (mod m)

This theorem is very obvious.

Linear operation theorem:

If A≡b (mod m), C≡d (mod m) Then:

(1) a±c≡b±d (mod m)

(2) A * c≡b * d (mod m)

If you want to see the proof of this theorem, see: http://baike.baidu.com/view/79282.htm

So:

7≡7 (mod 12)

(-2) ≡10 (mod 12)

7-2≡7 + ten (mod 12)

Now we have a negative number and find its positive number as the remainder. But not 7-2 = 7+10, but 7-2≡7 + (mod 12), that is, the remainder of the calculated result is equal.

Next go back to the question of binary, take a look: 2-1=1 problem.

2-1=2+ (-1) = [0000 0010] Original + [1000 0001] original = [0000 0010] Anti + [1111 1110] Inverse

First, 1 of the anti-code representation is 1111 1110. If [1111 1110] is considered to be the original code, then [1111 1110] The original =-126, where the sign bit is removed, which is considered to be 126.

The following rules have been found:

( -1) MoD 127 = 126

126 MoD 127 = 126

That

(-1) ≡126 (mod 127)

2-1≡2+126 (mod 127)

2-1 the remainder of the 2+126 is the same as the result! And this remainder, formally our expectation of calculation results: 2-1=1

So the inverse of a number is actually the same remainder of the number for a membrane. And this film is not our binary, but the maximum value that can be expressed! It's like a clock, and you can always find the right value in the range you're looking for in a round!

The 2+126 is obviously the equivalent of a clock turning round, and because the sign bit is involved in the calculation, just as the highest level of overflow to form the correct results.

Since the anti-code can be subtraction into addition, now the computer uses the complement it? Why add 1 on the basis of anti-code and get the correct result?

2-1=2+ (-1) = [0000 0010] Original + [1000 0001] original = [0000 0010] Complement + [1111 1111] Complement

If you take [1111 1111] as the original code, remove the sign bit, then:

[0111 1111] Original = 127

In fact, on the basis of anti-code +1, just equivalent to increase the value of the membrane:

( -1) MoD 128 = 127

127 MoD 128 = 127

2-1≡2+127 (mod 128)

At this point, the dial corresponds to one round of every 128 ticks. So the minimum and maximum values of the results of the operation in the complement should be [-128, 128].

But because of the special case of 0, there is no way to represent 128, so the range of complement is [-128, 127]

Reference: http://www.cnblogs.com/zhangziqiu/archive/2011/03/30/ComputerCode.html

http://blog.csdn.net/macky0668/article/details/3917878

The original code, the complement indicates

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.