Original code, anti-code, complementary explanation

Source: Internet
Author: User

Reprinted from:
Http://www.cnblogs.com/zhangziqiu/archive/2011/03/30/ComputerCode.html

This article explains the computer's original code, anti-code and complement. And in-depth exploration of why the use of anti-code and complement, as well as further proof of why you can use the inverse code, complementary addition to calculate the subtraction of the original code. If there is a wrong part of the argument, please help me to correct you! I hope this article will help you learn the basics of computer!

I. Number of machines and truth values
Before you learn the original code, the inverse code and the complement, you need to understand the concept of machine number and truth value first.

1. Number of machines

A binary representation of a number in a computer, called the number of machines. The number of machines is signed, in the computer with a number of the highest bit of the symbol, positive 0, negative number is 1.

For example, in decimal number +3, the computer word length is 8 bits, converted to binary is 00000011. If it is-3, it is 10000011.

So, here's 00000011 and 10000011 is the number of machines.

2. Truth value

Because the first bit is the sign bit, the form value of the machine number is not equal to the true value. For example, the above signed number 10000011, its highest bit 1 is negative, its true value is-3 instead of the form value 131 (10000011 is converted to decimal equals 131). So, for the sake of differentiation, the true value of the number of machines with the sign bit is called the truth of the machine number.

Example: 0000 0001 Truth value = +000 0001 = +1,1000 0001 True Value =–000 0001 =–1

Two. Original code, anti-code, complement the basic concept and calculation method.
Before we explore why machines need to be complementary, let's first understand the concepts of the original code, the inverse code, and the complement. For a number, the computer is stored with a certain encoding. The original code, anti-code, and the complement is the machine to store a specific number of encoding method.

    1. Original code

The original code is the absolute value of the symbolic bit plus the truth, that is, the first digit represents the symbol, and the remaining bits represent the values. For example, if it is a 8-bit binary:

[+1] original = 0000 0001

[-1] original = 1000 0001

The first bit is the sign bit. Because the first bit is the sign bit, the range of values for the 8-bit binary number is:

[1111 1111, 0111 1111]

That

[-127, 127]

The original code is the most easily understood and calculated representation of the human brain.

    1. Anti-code

The inverse code is represented by:

The inverse of a positive number is its own

Negative number of the inverse code is on the basis of its original code, the sign bit is unchanged, the remaining bits are reversed.

[+1] = [00000001] original = [00000001] Reverse

[-1] = [10000001] original = [11111110] Reverse

It can be seen that if an inverse code represents a negative number, the human brain cannot visually see its value. It is usually converted to the original code and then calculated.

    1. Complement

The complement is expressed in the following ways:

A positive complement is its own

The complement of a negative number is on the basis of its original code, the sign bit is unchanged, the rest of you take the counter, the last +1. (That is, on the basis of the anti-code +1)

[+1] = [00000001] original = [00000001] counter = [00000001] Complement

[-1] = [10000001] original = [11111110] counter = [11111111] Complement

For negative numbers, the complement representation is also the human brain can not intuitively see its value. It is also often necessary to convert the original code to calculate its value.

Three. Why to use the original code, anti-code and complement
Before I began to study, my study advice was to first "memorize" the original code, anti-code and complement of the expression and calculation method.

Now we know that a computer can have three encoding methods to represent a number. For positive numbers, the results are the same for three encoding methods:

[+1] = [00000001] original = [00000001] counter = [00000001] Complement

So there's no need to explain too much. But for negative numbers:

[-1] = [10000001] original = [11111110] counter = [11111111] Complement

Visible source code, anti-code and complement is completely different. Since the original code is directly recognized by the human brain and used to calculate the representation, why there are anti-code and complement it?

First of all, because the human brain can know that the first bit is the sign bit, in the calculation we will select the value of the truth region according to the sign bit. (The concept of truth is at the beginning of this article). However, for computers, the multiplier is the most basic operation, to be designed as simple as possible. Computer identification of "sign bit" will obviously make the computer's basic circuit design become very complex! So people came up with a way to take symbolic bits into the operation. We know that by subtracting a positive number from the algorithm equals adding a negative number, namely: 1-1 = 1 + (-1) = 0, so the machine can only add without subtraction, so the design of the computer operation is simpler.

So people began to explore the use of symbolic bits to participate in operations, and only preserve the method of addition. First look at the original code:

Expression for calculating decimal: 1-1=0

1-1 = 1 + (-1) = [00000001] Original + [10000001] original = [10000010] Original = 2

If the symbol is represented in the original code, it is obvious that the result is incorrect for subtraction. This is why the computer does not use the original code to represent a number.

In order to solve the problem of the original code subtraction, there is an inverse code:

Expression for calculating decimal: 1-1=0

1-1 = 1 + (-1) = [0000 0001] Original + [1000 0001] original = [0000 0001] Anti + [1111 1110] anti = [1111 1111] reverse = [1000 0000] Original = 0

It is found that the true value of the result is correct by inverse code calculation and subtraction. The only problem is in the "0" of this particular value. Although it is understood that +0 and 0 are the same, the 0 notation does not make any sense. And there will be [0000 0000] Original and [1000 0000] The original two codes represent 0.

So the complement appeared, solved 0 symbols and two coding problems:

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

So 0 is denoted by [0000 0000], and 0 of the previous problem does not exist. And you can use [1000 0000] to represent-128:

(-1) + (-127) = [1000 0001] Original + [1111 1111] original = [1111 1111] complement + [1000 0001] complement = [1000 0000] Complement

The result of 1-127 should be-128, in the result of the complement operation, [1000 0000] The complement is-128. But note that because it is actually using the complement of the previous-0 to represent-128, so-128 does not have the original code and the inverse code representation. (The complement of 128 is [1000 0000] The original code is [0000 0000], which is incorrect)

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:

clip_image001

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]

I have been not good at maths, so if there is a wrong place in the text please include more, lots of advice!

Zhang Zixiu
Source: http://www.cnblogs.com/zhangziqiu/
This article is copyright to the author and the blog Park, Welcome to reprint, but without the consent of the author must retain this paragraph, and in the article page obvious location to the original link, otherwise reserves the right to pursue legal responsibility.

Original code, anti-code, complementary explanation

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.