C Language Learning Notes Sharing--original code, anti-code and complement

Source: Internet
Author: User

First, what is the original code, anti-code and complement
We know that the signed numbers stored inside the computer are stored in complement form and are computed in the form of complement. What is the complement of a number? Why use the complement? This starts with the number of the original code, the inverse code. We take the integer number as an example and assume that the word length is 8 bits.
1, the original code
The original code of the integer x means: Its sign bit is 0 for positive, 1 for negative, and its numeric part is the binary number of the absolute value of x. The original code of x is usually denoted by an "x". Such as:
"+100" original =01100100 "+0" original =00000000
"-100" original =11100100 "-0" original =10000000
Note: In the original code, there are two representations of 0.
The original code notation is simple and easy to understand, and the true value (with the symbol number itself) conversion is convenient, as long as the symbol can be restored, but when the two positive subtraction or the number of different symbols added, you must compare two numbers which absolute size, in order to determine who minus who, can be determined whether the result is positive or negative, so the original
2, anti-code
X's anti-code refers to: for positive numbers, the inverse code is the same as the original code, for negative numbers, the sign bit is unchanged, its value bit x is reversed (1 change 0,0 to 1). The inverse of x is usually represented by an "x" inverse. Such as
"+100" counter = 01100100 "+0" anti-=00000000
"-100" counter = 10011011 "-0" anti-=11111111
Note: In the anti-code, 0 also has two kinds of representations.
The inverse code is also inconvenient, and is often used as an intermediate transition to complement.
3. Complement
The complement of X means: for positive numbers, the complement is the same as the original code, and for negative numbers, the sign bit is unchanged, and the absolute value of the digit x is reversed at the lowest bit plus 1. The complement of x is usually denoted by an "X" complement, in effect, "X" is the "x" Counter +1. Such as:
"+100" complement = 01100100 "+0" complement =00000000
"-100" complement = 10011100 "-0" complement =00000000
Note: In the complement, 0 has a unique encoding, and "+0" complements = "0" To fill = 00000000.

The complement operation is simple and convenient, and the sign bit can be used as a participating operation of the data, and it is not necessary to deal with it separately; the binary subtraction is implemented by its complementary complement, simplifying the hardware circuit.




Second, the meaning of complement
First, let's look at a few examples.
Example 1 uses 8-bit binary numbers to represent +0 and-0, respectively.
Solution: We know that for signed numbers, we specify the highest bit as the sign bit, 0 for positive, and 1 for negative numbers. The remaining bits are numeric digits and are used to denote the size of the number.
So +0 is represented as 0000 0000, and 0 is 1000 0000.
"Example 2" calculates the result of 9-6.
Solution: We know: 9-6=9+ (-6) =3
0000 1001
+1000 0110
1000 1111
The result is-15, obviously wrong.
And if we use the complement to calculate?
We know that 9 of the complement is 0000 1001,-6 's complement is 11111010, re-operation,
0000 1001
+1111 1010
1 0000 0011
The highest bit of 1 overflows, and the remaining 8-bit binary represents the complement of 3. The result is 3, correct.
"Example 3" analyzes the running results of the program.
Main ()
{int a=100,b=-1;
Printf ("a=%d,%x,%o,%u\n", a,a,a,a);
Printf ("b=%d,%x,%o,%u\n", B,b,b,b);}
Operation Result:
a=100,64,144,100
b=-1,ffff,177777,65535
In "Example 1," Why do the same 0 have two different ways of representing each other?
"Example 2", why the first method of calculation is wrong, and the use of complementary to calculate the result is correct?
In "Example 3", why is 1 the result of 16 binary, octal and unsigned integer output become ffff,177777,65535?
This is because, in a computer system, values are all represented by a complement (storage).
Main reasons:
1, unified 0 of the code;
2, the symbol bit and other numerical values are uniformly processed;
3, the subtraction operation into the addition operation;

4. If the highest bit (sign bit) has a carry, the carry is discarded when the sum of the numbers represented by a complement is two.




Three, the calculation of complement
1. Definition method:
(1) Positive complement: Same as the original code.
The complement of "Example 1" +9 is 00001001.
(2) The complement of the negative number: The sign bit is 1, the remainder is the absolute value of the original code bitwise negation; Then the whole number is added to 1.
"Example 2"-9 of the complement.
Because the given number is negative, the sign bit is "1".
After seven bits: +9 of the original code (0001001) → bitwise inversion (1110110) → plus 1 (1110111)
So 7 of the complement is 11110111.
2, check 0 law
The original code is used to represent the size of the numerical value, is a positive logic, the calculation of the original code we mainly calculate the number of 1, and the inverse code and complement is used to denote the size of the numerical value, belongs to the negative logic, so we can take the inverse logical thinking to understand, by calculating the number of 0 to find a negative complement. Since the complement is added to the inverse code based on a 1, so the number of 0 should be less than the absolute value of the original number 1.
For example: 5 of the complement
The number of zeros should be 4, so the complement of 5 is: 11111011.
Another example: 97 of the complement
The number of zeros should be 96, 96=64+32, the corresponding weight value bit is 0, the remaining bit is 1. So the complement of 97 is: 10011111.
3, 0 subtraction
The complement of negative numbers = Full 0-The original code of positive numbers, such as-5 of the complement of =0-5.
With the hexadecimal calculation of this number, the resulting result is converted to binary.
For example: 5 of the complement
Algorithm 1: Algorithm 2:
00000000 00H
﹣00000101﹣05h
11111011 0 FBH
4. Find 1 methods
For example: 15 of the complement
First step: +15:00001111
Step two: Find the first 1 from right to left and reverse all the bits on the left.
11110001
One more example to verify: 64 complement
+64:01000000

11000000




Iv. several additions
1, it is known that a number of the complement, the operation of the original code in two cases:
(1) If the sign of the complement is "0", the expression is a positive number, the original code is the complement.
(2) If the sign of the complement is "1", indicating a negative number, then the complement of the given complement is the required source code.
"Example 3" is known as a complement of 11111001, the original code is 10000111 (-7).
Because the sign bit is "1", it indicates a negative number, so the bit is unchanged and remains "1".
The remaining seven digits 1111001 are reversed to 0000110;
Plus 1, so it's 10000111.
2. Range of values

The range of the number represented by the 8-bit binary source code is -127~+127;




Five, Summary:
1, the original code notation
In addition to the sign bit, the other bits are the absolute values of the numbers, and this scheme is called the "original code" notation. For example:
+20 of the original code: 0 000 0000 0001 0100
-20 of the original code: 1 000 0000 0001 0100
2, anti-code notation
In addition to the symbol, the inverse code representation of the negative number is based on the original code of the other binary negation, and the positive number of the inverse code is the same as the original code. Such as:
+20 The anti-code is: 0 000 0000 0001 0100
-20 of the anti-code is: 1 111 1111 1110 1011
3. Complement notation
The complement representation of a negative number is added 1 on the basis of the inverse code, and the inverse of the positive number is the same as the original code.
4, why the computer generally use the complement notation
The original code, anti-code and the complement is due to the negative number of three schemes, three schemes, the original code is most suitable for the multiplication class operation, the complement is suitable for the addition and subtraction of the operation, and the inverse code is not very good and multiplication, because the frequency of the addition and subtraction operation is much higher than the multiplication operation, so most computer systems So all of the computer's addition and subtraction operations are to convert the corresponding number to complement, and then the operation.


The original article source: C + + technology network http://www.cjjjs.cn, read more original articles, welcome to visit the C + + technology network.

C Language Learning Notes Sharing--original code, anti-code and 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.