Soft Test--original code, anti-code, complement

Source: Internet
Author: User

Recently in the preparation of soft test software designer exam, there are a lot of basic software engineering knowledge and computer based knowledge, yesterday, when the real problem encountered a problem,

The topics are as follows:

If the "2X" complement is "90H", then the truth of X is ()

a.72 b.-56 c.56 d.111

There are several concepts involved in the topic, first of all to understand

One, the original code, complement, anti-code

First of all, a number in the machine is expressed in binary form, also known as the number of machines, it is signed, the highest bit of the symbol, positive 0, the plural is 1.

True value, because the number of machines has a symbol, so its form value does not equal its real values, so in order to distinguish, the machine number is true value shouting truth.

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

Original code

The original code is the absolute value of the symbol bit plus the truth, that is, the first digit represents the symbol, the remaining representation

  

[+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]

  

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.

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.

Why 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 a complement representation

Soft Test--original code, anti-code, complement

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.