Negative computer Complement

Source: Internet
Author: User
Tags modulus

 

In a computer system, all values are represented (stored) by supplementary codes ).
The main reason is that the complement code can be used to process the symbol bit and other digits in a unified manner. At the same time, the subtraction can also be processed by addition. The other two are supplemented.
If the highest bit (symbol bit) has a forward bit, the carry is discarded.
2. The conversion process of the complement code is almost the same as that of the original code.
The value complement representation is also divided into two situations:
(1) positive complement: the same as the original code.
For example, the complement code of + 9 is 00001001.
(2) Complement of negative numbers: the sign bit is 1, and the rest is the original code of the absolute value of the number is reversed by bit; then the whole number is added to 1.
For example, the-7 complement: because it is a negative number, the symbol bit is "1", the whole is 10000111; the other seven digits are the absolute value of-7 + the original code of 7
The bitwise decimal value of 0000111 is 1111000, and the value of-7 is 11111001.
If you know the complement of a number, you can perform the following operations on the source code:
(1) If the sign bit of the complement code is "0", it indicates a positive number. Therefore, the complement code is the original code of the number.
(2) If the sign bit of the complement code is "1", it indicates a negative number. The operation to evaluate the source code can be: the sign bit is 1.
And then add 1 to the entire number.
For example, if a complement code is known as 11111001, the original code is 10000111 (-7): Because the symbol bit is "1", it indicates a negative
The number, so the bit remains unchanged, still "1"; the other 7 digits are reversed to 1111001; Add 1, so it is 0000110.
In the "Idle original code, reverse code, and complement code" file, we did not mention a very important concept "model ". Here I will give a brief introduction to the "model"
Concept:
"Module" refers to the counting range of a metering system. Such as clock. A computer can also be seen as a metering machine, and it also has a measurement model.
There is a "MODULE ". For example:
The Metering range of the clock is 0 ~ 11, modulo = 12.
The N-bit computer metering range is 0 ~ 2 (n)-1, modulo = 2 (n ). [Note: N indicates the Index]
The "module" is essentially the amount of overflow produced by the metering device. Its value cannot be expressed on the metering device, and the metering device can only represent the output mode.
Remainder. Any metered tool with modulus can be reduced to addition.
For example, if the current hour hand points to 10 and the exact time is 6, there are two ways to adjust the time:
One is a four-hour switch, that is, 10-4 = 6.
The other is to distribute the traffic for 8 hours: 10 + 8 = 12 + 6 = 6
In a 12-mode system, the addition of 8 is the same as the subtraction of 4. Therefore, any subtraction operation can be replaced by the addition of 8.
For "modulo", 8 and 4 complement each other. In fact, in a 12-Mode System, 11, and 6 both have this feature.
. The common feature is that the sum of the two equals the modulo.
Computers have the same concept and method. For N-bit computers, if n = 8, the maximum number can be 11111111.
Addition 1 is called 100000000 (9 bits), but because there are only 8 bits, the highest bits 1 is naturally lost. 00000000 again, so the 8-bit binary system
The modulo is 2 (8 ). In such a system, the subtraction problem can also be converted into an addition problem. You only need to use the corresponding population to represent the reduction.
. The complement number is used to process the computer logarithm, that is, the complement code.

Two other concepts
One's complement refers to positive number = original code, negative number = reverse code.
Two's complement refers to the complement code that is usually referred.

The number is expressed in binary form in the computer.
The number is divided into two types: signed and unsigned.
The original code, reverse code, and complement code are all represented by signed vertices.
The highest bit of a signed number is the sign bit. 0 indicates positive, and 1 indicates vice.

The following uses an 8-digit integer as an example,

The original code is the binary form of the number.
For example
0000001 is + 1
1000001 is-1

The positive and supplementary codes are the same as the original codes.

The negative number is the inverse of the original code except the symbol bit.
[-3] Reverse = [10000011] Reverse = 11111100
The complement of a negative number is to reverse all of its original codes except the symbol bit, and then add 1 to the last digit.
[-3] fill = [10000011] fill = 11111101
A number and its complement are reversible.

Why do I need to set up a supplementary code?

The first is to allow computers to perform subtraction:
[A-B] fill = a fill + (-B) Fill

The second reason is to unify positive 0 and negative 0.
Positive zero: 00000000
Negative zero: 10000000
Both numbers are actually 0, but their original codes have different representation.
But their supplementary codes are the same, all of which are 00000000.
Note that if there is a forward carry after + 1, it should be carried forward, including the sign bit! (This is different from the anti-code !)
[10000000] supplement
= [10000000] anti + 1
= 11111111 + 1
(1) 00000000
= 00000000 (the highest bit overflows, And the symbol bit changes to 0)

Someone will ask
10000000 Which of the following is the complement number?
In fact, this is a rule. This number indicates-128.
So the range of the N-bit complement can be
-2 ^ (n-1) to 2 ^ (n-1)-1
One more than the n-bit original code

Another example:
1011
Original code: 01011
Back code: 01011 // when positive, back code = original code
Complement: 01011 // if positive, the complement = original code

-1011
Original code: 11011
Back code: 10100 // negative, the back code is the back code
Complement: 10101 // negative, the complement is the back of the original code + 1

0.1101
Original code: 0.1101
Back code: 0.1101 // when positive, back code = original code
Complement: 0.1101 // if positive, the complement = original code

-0.1101
Original code: 1.1101
Back code: 1.0010 // negative, the back code is the back code
Complement: 1.0011 // negative, the complement is the back of the original code + 1

Summary:
In a computer, the number of vertices can be expressed in three forms: original code, reverse code, and complement code.

The so-called original code is the Binary Fixed-point notation introduced earlier, that is, the highest bit is the symbol bit, "0" indicates positive, "1" indicates negative, and the remaining bit indicates the value size.

The anticode notation specifies that the anticode of a positive number is the same as the original code; the anticode of a negative number is the inverse of the original code by bit, except for the symbol bit.

The complement expression specifies that the positive value is the same as the original value. The negative value is added with 1 at the end of the reversed value.

1. Representation of the source code, reverse code, and complement code

(1) original code: directly add a sign before the value.

For example, the symbol bit value

[+ 7] original = 0 0000111 B

[-7] original = 1 0000111 B

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

[+ 0] original = 00000000b [-0] original = 0000000b

B. 8-bit binary original code representation range:-127 ~ + 127

2) Anti-code:

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

Negative number: the inverse code of a negative number. The symbol bit is "1", and the value part is reversed by bit.

For example, the symbol bit value

[+ 7] inverse = 0 0000111 B

[-7] Reverse = 1 1111000 B

Note: A. There are two anti-code forms for the number 0, that is

[+ 0] = 00000000b

[-0] anti = 111111b

B. The range of 8-bit binary anticode:-127 ~ + 127

3) representation of the complement

1) Concept of a module: it refers to a unit of measurement as a module or module. For example, the clock is a 12-in-12 counting cycle. On the clock, the hour hand plus (positive dial) 12 of the Whole Digit or minus (reverse dial) 12 of the Whole Digit, the hour hand position remains unchanged. After two o'clock, it becomes (afternoon) (14 = 14-12 = 2 ). Start from and dial 10 cells counterclockwise, that is, minus 10 hours. You can also set the value to 2 cells clockwise (plus 2 hours) from ), that is, 2 points (0-10 =-10 =-10 + 12 = 2 ). Therefore, on the premise of mod 12,-10 can be mapped to + 2. It can be seen that for a 12-module loop system, the effects of adding 2 and reducing 10 are the same. Therefore, in a 12-module system, all operations that subtract 10 can be replaced by adding 2, which converts the subtraction problem into an addition problem (Note: In the hardware structure of a computer, only the adder is used, therefore, most operations must be converted to addition ). 10 and 2 complement each other in mod 12.

Similarly, the computation components and registers of a computer are limited to a certain number of characters (assuming the word length is 8). Therefore, the computation is also a modulo operation. When the counter is filled with 8 bits, that is, 256 bits, it will overflow and count from the beginning. The amount of overflow is the counter model. Obviously, the 8-bit binary number has a modulus of 2 ^ 8 = 256. In calculation, two complementary numbers are called "complement codes ".

2) positive value: the positive value is the same as the original value.

Negative number: the complement of the negative number is the sign bit "1", the value part is reversed by bit and then added 1 to the last bit. That is, "anti-code + 1 ".

For example, the symbol bit value

[+ 7] fill = 0 0000111 B

[-7] fill = 1 1111001 B

The complement code is an important encoding method in a microcomputer. Note the following:

A. After completing the code, you can easily convert the subtraction operation into an addition operation, which simplifies the operation process. The positive value is the true value of the number it represents, while the negative value is not the true value of the number it represents. The result is still the complement code.

B. Unlike the source code and reverse code, the value 0 has only one complement code, that is, [0] fill = 00000000b.

C. If the length is 8 characters, the value range of the complement code is-128 ~ + 127; when performing the complement operation, note that the result should not exceed the range of the number that can be expressed by the complement.

Related Article

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.