Complement knowledge learning.

Source: Internet
Author: User
Tags binary to decimal
Complement
Encyclopedia business cards
Two's complement 1. in computer systems, all values are represented (stored) by a supplemental code ). 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. In addition, when two numbers are added, if the highest bit (symbol bit) has an incoming bit, the carry is discarded. 2. The conversion process of the complement code is almost the same as that of the original code.

Directory

Complement Overview (1) positive complement
(2) complement a negative number
(3). the absolute value of the complement code (called the true value)
Addition and subtraction of algebra 1. complement Addition
2. complement and Subtraction
3. complement Multiplication
An overview of complement algebra explanations (1) positive complement
(2) complement a negative number
(3). the absolute value of the complement code (called the true value)
Addition and subtraction of algebra 1. complement Addition
2. complement and Subtraction
3. complement Multiplication
Algebraic interpretation of complement code
Expand and edit this paragraph completion Summary
Evaluate the complement of a given value in the following two cases:
(1) positive complement
Same as the original code. [Example 1] The Supplementary Code of + 9 is 00001001. (Note: This + 9 complement represents the complement using the 8-bit binary mechanism. There are many methods to represent the complement, and there is also a 16-bit binary complement representation, and 32-bit binary complement representation .)
(2) complement a negative number
The symbol bit is 1, and the other digits are the original codes of the absolute value of the number, which are reversed by bit. Then the entire number is added with 1. The same number is different in different complement representation. For example, the complement code of-15 is 11110001 in the 8-bit binary system, but in the case of 16-bit binary complement representation, it is 1111111111110001. The complement conversion involved in this complement overview converts a number into a 8-bit binary complement by default. Each complement expression can only represent a limited number. [Example 2] calculate the-7 complement. Because the given number is negative, the symbol bit is "1 ". The last seven digits: the original code of + 7 (0000111) → bitwise reverse (1111000) → plus 1 (1111001) So the complement code of-7 is 11111001. If you know the complement of a number, the operation of the original code is divided into two situations: (1) if the sign bit of the complement code is "0", it indicates a positive number, and its original code is the complement code. (2) If the sign bit of the complement code is "1", it indicates a negative number, the complement code for the given complement code is the required original code. Another method is to calculate the complement of a negative number as follows: for example, to obtain the complement of-15, Step 1: + 1111, Step 2: bitwise round (1 is changed to 0, 0 to 1 ), then add 1 to the end. 11110001 another example is used to verify that the-64 complement + 64: 01000000 11000000 [Example 3] If a 11111001 complement is known, the original code is 10000111 (-7 ). Because the symbol bit is "1", it indicates a negative number, so the bit remains unchanged and is still "1 ". The other seven digits are rounded up to 1111001; then 1 is added, 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 introduce the concept of "MODULE": "MODULE" refers to the counting range of a metering system. Such as clock. A computer can also be seen as a metering machine. It also has a measurement model ". 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 ). 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 remainder of the output model. 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, you can adjust the time in either of the following ways: 10-4 = 6 the other is to dial 8 hours: 10 + 8 = 12 + 6 = 6 in a 12-mode system, the effects of adding 8 and reducing 4 are the same, therefore, all operations that subtract 4 can be replaced by 8. For "modulo", 8 and 4 complement each other. In fact, in 12-mode systems, 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. If n-bit computers are set to n = 8, the maximum number that can be expressed is 11111111. If we add another 1, it is called 100000000 (9 bits). However, because there are only 8 bits, the maximum 1 is naturally lost. The modulo of the 8-bit binary system 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 complement expression for the reduction. The complement number is used to process the computer logarithm, that is, the complement code. In the other two concepts, one's complement refers to positive number = original code, negative number = reverse code, and two's completions refer to the complement code. Decimal Complement Method: A simple method, where the symbol bit remains 1 unchanged, the value bit remains unchanged from the first 1 on the right and the 0 on the right, and the bitwise on the left is reversed.
(3). the absolute value of the complement code (called the true value)
[Example 4] The complement code of-65 is 10111111. If 10111111 is directly converted to decimal, it is found that the result is not-65, but 191. In fact, in a computer, if it is a binary number and its leftmost bit is 1, we can determine it as a negative number and represent it with a complement code. To obtain the absolute value (called the true value) of a negative binary number, you can obtain the true value as long as you take the inverse value (including the sign bit) and Add 1 to it. For example, if the binary value is 10111111 (-65's complement code), you can obtain the reversed value: 01000000 plus, (+ 65's complement code)
Edit this section of algebra addition and subtraction
1. complement Addition
[X + y] fill = [x] fill + [y] fill [Example 5] x = + 0110011, y =-0101001, [x + y] fill [x] fill = 00110011 [y] fill = 11010111 [x + y] fill = [x] fill + [y] fill = 00110011 + 11010111 = 00001010 note: because the bit length of the generator in the computer is fixed, the highest bit carry generated in the above calculation will be lost, so the result is not 100001010, but 00001010.
2. complement and Subtraction
[X-Y] fill = [x] fill-[y] fill = [x] fill + [-y] fill where [-y] fill is called negative fill, the method for calculating the negative population is: bitwise inverse of all bits of the original code of the absolute value of the negative number; then the whole number is added to 1. (Restore the original explanation. Ask passers-by to really understand and verify it before modifying it. To avoid misleading the public. In addition, Example 6 is not typical, and Example 7 is added .) [Example 6] 1 + (-1) [decimal] 1 original code 00000001 is converted into a complement code: 00000001-1 original code 10000001 is converted into a complement code: 11111111 1 + (-1) = 0 00000001 + 11111111 = 00000000 00000000 to 0 0 = 0 in decimal format. Therefore, the operation is correct. [Example 7 increment]-7-(-10) [decimal]-7 complement: 11111001-10 complement: 11110110-(-10 ): bitwise inversion plus 1 is actually the complement code of its negative value, which is 00001010-7-(-10) =-7 + 10 = 3 11111001 + 00001010 = 00000011 to 3 in decimal format.
3. complement Multiplication
Set quilt multiplier [x] fill = x0.x1x2 ...... Xn-1, multiplier [y] fill = y0.y1y2 ...... Yn-1, [x * Y] fill = [x] fill X [y] fill, that is, the multiplier (by the multiplier) multiplied by the complement is equal to the multiplication of the complement.
Edit the algebraic interpretation of the complement in this section
Any number can be expressed as-A = 2 ^ (n-1)-2 ^ (n-1)-A. If a is a positive number,-A is a negative number. Based on the binary to decimal number method, we can express a as: a = K0 * 2 ^ 0 + K1 * 2 ^ 1 + K2 * 2 ^ 2 + ...... + K (n-2) * 2 ^ (n-2), the Nth (n-1) bit is not included in the sign bit. Here K0, K1, K2, K (n-2) is 1 or 0, and here set the binary number of A to N bits, that is, the modulo is 2 ^ (n-1 ), the second expansion of 2 ^ (n-1) is: 1 + 2 ^ 0 + 2 ^ 1 + 2 ^ 2 + ...... + 2 ^ (n-2), and formula:-A = 2 ^ (n-1)-2 ^ (n-1)-A, 2 ^ (n-1) -A is substituted into a = K0 * 2 ^ 0 + K1 * 2 ^ 1 + K2 * 2 ^ 2 + ...... + K (n-2) * 2 ^ (n-2) and 2 ^ (n-1) = 1 + 2 ^ 0 + 2 ^ 1 + 2 ^ 2 + ...... + 2 ^ (n-2) Two formula, 2 ^ (n-1)-A = (1-k (n-2) * 2 ^ (n-2) + (1-k (n-3 )) * 2 ^ (n-3) + ...... + (1-k2) * 2 ^ 2 + (1-k1) * 2 ^ 1 + (1-k0) * 2 ^ 0 + 1, the transformation in this step is exactly the algebraic principle of the rule of inverse plus 1. Because K0, K1, K2, K3 ...... 1-K0, 1-K1, and 1-K2 operations are bitwise in binary. Why do we add 1? It is 2 ^ (n-1) the second expansion has another reason. -A = 2 ^ (n-1)-2 ^ (n-1)-A, and-2 ^ (n-1) is not explained. This is the first 1 in the complement code, when the first 1 is converted to decimal, it must be multiplied by 2 ^ (n-1), which is the n-bit binary modulo. You cannot paste formulas, so it looks very troublesome. It is very convenient to write an algebraic sub-statement. Note: N-bit binary, the highest bit is the symbol bit, so the value range is-2 ^ (n-1) -- 2 ^ (n-1)-1, so the modulo is 2 ^ (n-1 ). The preceding 8-bit binary modulo is 2 ^ 8 because the highest bit is not a signed bit, and the value range is 0-2 ^ 8-1.

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.