The purpose of the original code's complement code shift

Does not involve fixed-point and floating-point operations. The range is only in the data representation of integers.

Use metaphors and use similar thinking to understand the commonalities. This is what I want to write this article.
The calculation method is designed, and the thinking and purpose of the method is the main line when we study.
Tang Shuofei’s textbook is too overestimating my IQ. It comes up with decimals, and the complements are all abstract calculations. They don’t give a few examples (this, textbook for beginners?)......


We first use 8-bit binary numbers to represent the value

Truth value Original code Complement code (when negative number, complement code +1) Inverse code (when negative, the original code is inverted except for the sign bit) Shift code (complement code sign bit copy reverse)
-5 1,000 0101 1,111 1011 1,111 1010 0,111 1011
+5 0,000 0101 0,000 0101 0,000 0101 1,000 0101

Note that the "comma" is used to distinguish the sign bit from other bits and does not actually exist.


Clearly put forward a few problems to be solved

1. The sign bit will participate in the operation
2. All subtraction operations need to be added
3. Limited storage bits

The principle of solving problem 1- mapping. The mapping
from [-128, 127] to [0, 255] is manually specified, so that "only positive numbers represent negative numbers" can
be specified and a mapping (shift code) that is easy to directly compare sizes can be specified. Comparison of size.

The principle of solving problems 2 and 3- modulo operation
After the modulus is "complement", after adding the mapping, it corresponds to the corresponding positive and negative numbers. The
modulus itself is a kind of digit limit

Similar to the number of angles,
in the same circle 60°= -300°, the modulo operation, where the modulus is 360. (In the range of integers, from 0 to 359, calculated as 360=0)
in one byte 8 bits Except for the 7 digits of the sign bit,
1111 1111=0000 0000, modulo operation, here the modulus is 1 0000 0000 (there is no number exceeding this modulus, have you seen a certain digit in decimal?)

Put another table to compare and comprehend

code The true value represented by the original code (unsigned bit) The true value represented by the original code (signed bit) The true value represented by the complement code (whoever negates the +1 is the "code")
1111 1111 255 -127 Original code 1000 0001, which means -1

The purpose of designing this way of representation (encountering problems, inventing tools, solving problems)

Truth value

Give a number
3 (decimal)-true value

Unsigned binary number

Represent 3 (decimal) as binary
0011-unsigned binary number

Original code

But what to do if you have to consider the sign? Take the highest bit as the sign bit!
0,011-original code
Aha, then negative 3 (decimal) is
1,011-original code

Complement

What if I want to count 5 (decimal)-3 (decimal)?
0,101
1,011
These two numbers cannot be directly manipulated to get the result! !
Given a mapping, the positive number is still itself, and the negative number is modulo! Let the negative number plus its modulo value equal 1 0000! ——Realize the mapping from [-8, 7] to [0, 15]
(the modulus is one bit greater than the highest bit, such as the decimal modulus is 10, no single bit can be greater than or equal to 10)

Complementary code (why inverted by bit and add one)
I like this blogger's sentence-"Because what you want is not 1+1=2, but why 1+1 is equal to 2. Of course, we don’t Discuss the problem of 1+1. We are discussing the complement."
This blog summarizes [positive number + (inverse code +1) = 0, negative number = (inverse code +1)]
-think so , The mapping of the positive number itself, the negative number first invert all the bits (including the sign bit),
then for example, the inverse code of 0010 (+2) is 1101, and because each bit is inverted, the result is 1111, in
order to make the positive number and Adding negative numbers "becomes 0", you can add 1 to become 1 0000, and the modulus is 0000, that is, "the opposite numbers add to zero"

Thus obtained 3 (decimal) to 10 000 modulo value = 10000-1011 = 00101
0,101
sign bit again according to a negative [10000 = 1111 + 1, can be seen as "negated +1"]
1,101-complement

6 (decimal)-3 (decimal) can be
0,110
1,101 and the
sum is 10011, and then the modulus of 1 0000 is 0011 (it is a positive complement, and the positive complement is equal to the original code).
0,011-the original code of the calculation result (+3)

1 (decimal)-3 (decimal) can be
0,001
1,101 and the
sum is 1110, and then modulo 1 0000 is 1110 (a negative complement). [The one's complement calculation is still the complement of 10011, and when the original code is negative, you need
to "minus one" to get 1101, and then "
neg " to get 1,010 1,010 ——calculation result The original code (-2)

Frameshift

When comparing the original code,
0,011 (+3),
1,010 (-2) are
expressed as unsigned binary numbers.
0011 (3)
1010 (10)
is obviously unreasonable.
Given that the boundary values ​​have been specified, it can be determined that the numbers beginning with 1 are all negative numbers , All numbers starting with 0 are integers-just change it? ?
1011——+3 shift code
0010—— -2 shift code In
this way, the mapping of the original code to unsigned binary numbers is reasonable


nourishment:

Excerpted from: What are the generation, application, advantages and disadvantages of the original code, inverse code, and complement code? ——He Xinyu
The description of the origin of the complement can be summarized as follows:
1. In the computer, there is only an adder and no subtractor. All subtraction operations must be performed by addition .
2. Use the complement instead of the original number to transform the subtraction into addition.
3. The carry that appears is the modulus, and the carry at this time should be ignored.
4. In the binary system, how many digits are involved in the operation, and the modulus is how many 0s are added after the 1.
The complement is defined in accordance with this requirement: positive numbers remain unchanged, and negative numbers are modulo minus absolute values.

Excerpted from: What are the generation, application, advantages and disadvantages of the original code, inverse code, and complement code? —— Zhang Tianxing

The so-called original code is the machine number, which is a binary number with a sign bit added. The sign bit of a positive number is 0, and the sign bit of a negative number is 1. The data stored, processed and calculated in the computer is usually 8 bits, 16 bits, and 32 bits. Or 64-bit, here is the simplest 8-bit explanation. Note that the sign bit is one of the 8 bits, so the number that can be read intuitively is only 7 (only the last 7 digits can be expanded by weight). Someone may notice that the original code is flawed. It can only represent 255 states, because 00000000 (+0) and 10000000 (-0) are actually the same number, so the range of the original code has become -127 to +127. This is a problem. A magical complement is needed to solve it, because 10000000 is used to represent -128 in the complement.
The so-called one's complement is also called ones' complement in English. The 1 here is essentially the maximum value that can be represented in a finite-digit counting system. In 8-bit binary, it is 11111111, which is 1 bit. It is 9 in decimal, and FFF in 3-digit hexadecimal. The negation is also called the complement of one. You can get its negation by subtracting a number from the maximum number. It is easy to see that subtracting any number from 11111111 in binary is the bitwise negation of this number, and 0 is changed. 1, 1 becomes zero, so it is called the inverse code. The method of negating the original code is that the positive number remains unchanged, the negative number retains the sign bit 1 unchanged, and the remaining bits are inverted bit by bit.
The so-called complement is also called two's complement in English. This 2 refers to the capacity (modulus) of the counting system, which is the number of states that the counting system can represent. For a 1-bit binary number, there are only two states, 0 and 1, so the modulus is 10, which is 2 in decimal, and for 7-bit binary numbers, it is 10000000. This modulus is impossible to get, because the number of digits is one more. . Subtract a number (unsigned part) from the modulus to get the complement of this number. For example, 10000000-1010010=0101110. In fact, because 10000000=1111111+1, a slight change becomes (1111111-1010010)+1, so It can also be expressed as negating first and then adding 1. The method of summing up the complement is that the positive number remains unchanged, the negative number retains the sign bit, and the one's complement is added first.
Remember how to find one's complement, let's talk about calculations. Through the sign bit and value of the original code, we can quickly point out the number it represents, judge its positive and negative and perform four arithmetic operations. In contrast, the inverse code and the complement code are too obscure for people. If the original code is the digital language for humans, then the complement is the digital language of the computer. The computer does not need to know what is positive and negative, the size, these judgments are too complicated for it. In fact, the numbers stored, processed, and transmitted have only one form of complement . The addition, subtraction, multiplication, and division made by humans can be solved by adding and shifting in the computer. This comes from the inherent freedom of the complement system. The magical power of Qiahe's ingenious workmanship is also the focus of the following article.
For addition and subtraction, after obtaining the complement according to the above method, you can directly add it, but the sign bit must be involved in the operation when adding. Sometimes, the two sign bits are added or the carry from the lower bit is accepted. If overflow occurs, throw away the overflowed bit (we will explain why later), and the new sign bit determines the sign of the result. If 0 means positive, the result is the original code, if 1 means negative, the result is still You need to find the complement to get the original code.

Excerpted from: Complement code corresponds to 128.
Negative numbers are represented by complement code, which actually implements a mapping from [-128, 127] to [0, 255] . As follows:

+----------------------------+
| 255     -1       11111111  |
| 254     -2       11111110  |
| 246     -10      11110110  |
| 156     -100     10011100  |
| 129     -127     10000001  |
| 128     -128     10000000  |
| 127      127     01111111  |
| 100      100     01100100  |
| 10       10      00001010  |
| 2        2       00000010  |
| 1        1       00000001  |
| 0        0       00000000  |
+----------------------------+

Guess you like

Origin blog.csdn.net/sinat_27382047/article/details/105916458