Basic field: The original code, the inverse code and the complement

Source: Internet
Author: User
Tags modulus

Brief

Ashamed to say that although the first contact with the computer has learned the original code, anti-code and the complement of content, but recently relive the "What is this thing", it seems that the original only to deal with the exam. This article will try to tell them to understand, in case you forget again later.

Before going any further, let's make the following points clear:

1. The contents of this article are all for signed number integer;

2. For a signed number integer, its storage structure in the computer is the sign bit + truth field. Where the sign bit is 0 for positive number and 1 for negative number;

3. Q: Since already has the original code, then why also has to appear the anti-code, the complement and so on the numerical code way?

A: Because in order to reduce the computer physics circuit design difficulty, the decision to use addition to replace the subtraction operation (so there is no subtraction inside the computer), that is, 10-5 is replaced by the + + (-5), and anti-code, complement is used to solve the problem of the + + (-5).

True Form

The original code is to convert the decimal number directly into a binary number form. If the original code of 7 is 0111,-6 the original code is 1110.

Attention:

1. The original code is to distinguish between +0 and-0, +0 of the original code is 0000;-0 of the original code of 1000;

2. If the storage space is n bit, then the value range of the original code is -2n-1 ~ 2n-1.

The problem caused by the original code in an operation that replaces subtraction with addition:

For example, in the calculation 0 = 1-1 = 1+ (-1) = 0001 + 1001 = 1010 =-2, it is found that the operation by the original code will actually get 0 = =-2 results. Then the anti-code was introduced.

One ' s complement

The rules for converting the original code into an inverse code are as follows:

1. The inverse code of the positive integer source code is its own. If the original code 0001 of the inverse code is 0001;

2. The inverse code of the negative integer source code is to reverse the single digit of the original code truth domain. such as the original code 1010 of the inverse code is 1101.

Then the rule to convert the inverse code to the original code is as follows:

1. The inverse of the positive integer inverse code is its own. If the original code of anti-code 0001 is 0001;

2. The original code of the negative integer inverse code is to reverse the single-digit number of the inverse Code truth domain. If the original code of anti-code 1101 is 1010.

Attention:

1. Anti-code is to distinguish between +0 and-0, +0 of the anti-code for the 0111;-0 of 1111;

2. If the storage space is n bit, then the value range of the inverse code is -2n-1 ~ 2n-1.

The problem caused by an inverse code in an operation that replaces subtraction with addition:
For example, in the calculation 0 = 1-1 = 1+ (-1) = 0001 "Original code" + 1001 "original Code" = 0001 "anti-code" + 1110 "anti-code" = 1111 "anti-code" = 1000 "original Code" = 0, found through the inverse code to calculate the result of the 0 = = 0.

See that the result of using the inverse code operation is very close to the correct result, but 0 for us still does not have much meaning. Then the complement was introduced.

Both ' s complement

The rules for converting the original code into complement are as follows:

1. The complement of the positive integer source code is its own. If the original code 0001 of the complement is 0001;

2. The complement of the negative integer source code is to reverse the single digit of the original code truth domain, the whole +1 can be. If the original code 1010 of the complement is 1111.

Then the rules to convert the complement to the original code are as follows:

1. The complement is then obtained by the original code.

Attention:

1. The complement is not differentiated between +0 and-0, +0 and 0 of the complement is 0000;

2. If the storage space is n bit, then the value range of the inverse code is -2n ~ 2n-1.

3. Original code + its complement = 0.

Complement the result of an operation that replaces subtraction with addition:

For example in the calculation 0 = 1-1 = 1+ (-1) = 0001 "Original code" + 1001 "original Code" = 0001 "anti-code" + 1110 "anti-code" = 0001 "complement" + 1111 "complement" = 0000 "complement" = 0000 "original Code" = 0, found by the complement to calculate the knot The result is exactly right.

truth: Signed integers are actually stored in the form of a complementary code. So the value range of the C language int type on the 32-bit OS is: -2n ~ 2n-1. We can verify by following the C code snippet:

#include <stdout.h>#include<string.h>intMain () {intf =-1; unsignedLongl; inti; Chars[ +]; memcpy (&l, &f,4);  for(i = to; I >=0; --i) {       ifL2==1) {S[i]='1'; }       Else{S[i]='0'; } l/=2; } s[ +] =' /'; printf ("%s\n", s); return 0;}

The standard output is: 11111111111111111111111111111111 (if the original code should be 10000000000000000000000000001)

Here we have a certain degree of the original code, anti-code and the complement of the understanding, and through the traditional learning method of memorizing the correct questions should be no problem. But to justify it, you inevitably need to answer the following questions:

1. Why does the complement approach solve the problem caused by adding substitution subtraction?

Let's discuss and deduce it together.

Theory

Modulus

modulo is the counting range of a metering system, and the modulus is the amount of "overflow".

In the case of clocks, the counting range of a clock is 0~11, and the modulus (the amount of "overflow") is 12. Now we move the hour hand counterclockwise from 4 to 2, and move clockwise 10 to get the same result. The arithmetic formula is 4-2 and 4+10, at a glance 4-2 is obviously not equal to 4+10, then why the two results on the clock is the same?

That's "modulo" in mischief. When the result of the operation exceeds the counting range, the modulo operation is performed, and the 4+10=14>12,12 is 2=4+ (-2) after the modulo is obtained. At this point you will find that the complement of negative numbers must be positive , then the need to represent negative numbers in positive numbers is resolved. The next step is to verify that there is a problem in participating in the addition operation when the complement resolves to a positive number for negative numbers.

Before we go any further, let's look at the theory:

complement/complement/two complement , if a, B divided by M for modulo execution after the result is equal, then A and b complement each other, the formula: A≡b (mod m).

modulo formula : MoD = a-m * la/mj, where LJ stands for the "remove bounds" symbol. (Note:% is the operator that takes the remainder instead of the modulus, and the modulus and the remainder are essentially different)

The following JS to describe

/** * @description modulo * @method mod * @public * @param {number} O-operand * @param {#} M-Modulo, value range: numbers except 0 (integers, decimals, positive and negative numbers) * @returns {Number}-the symbol of the modulo result is consistent with the symbol of the modulo*/varMoD = (o/*Perand*/M/*Odulus*/) = {    if(0= = m)ThrowTypeError ('argument modulus must not be zero!')    returnO-m * Math.floor (o/k);/** * @description balance * @method rem * @public * @param {Number} dividend-divisor * @param {#} divisor-Dividend, value range: except 0 Words (integers, decimals, positive numbers, and negative numbers) * @returns {number} remainder-remainder, symbol consistent with divisor*/varrem = (dividend, divisor) = {    if(0= = divisor)ThrowTypeError ('argument divisor must not be zero!')    returnDividend-divisor * MATH.TRUNC (dividend/divisor)}

The theorem of the complement :

1. Reflexivity: A≡a (mod m)

2. Symmetry: If A≡b (mod m). Then B≡a (mod m)

3. Transitivity: If A≡b (mod m), B≡c (mod m). Then A≡c (mod m)

4. Add-On: If A≡b (mod m), C≡d (mod m). Then A+c≡b+d (mod m), a-c≡b-d (mod m)

5. Multiply the complement: if A≡b (mod m), C≡d (mod m). Then AC≡BD (mod m)

6. The absolute value of each complement is added equal to the modulus: if A≡b (mod m), then M = |a| + |b|

7. If both A and B are positive, mod (A + b, m) = mod (A, m) + mod (b, M)

Derivation

With the above modulus theory we can pull up the sleeves and start to deduce!

First assume that the data is now stored in n-bit bits, where the leftmost one is the sign bit, then the modulo is 2n-1. Then a positive integer is represented by a, B represents a negative integer, and C is the complement of B.

1. Sort out b≡c (mod 2n-1), a≡ (mod 2n-1)

2. Get A+b≡a+c (mod 2n-1) based on the addition of the complement formula

3. MoD (a+b, 2n-1) = mod (a+c, 2n-1)

4. The definition of the "model" refers to the counting range of a metering system, while the modulus is the amount of "overflow", which shows that the "overflow" operation in the process of operation is essentially the execution of the modulo operation. And our storage space is fixed to n-bit bits, so the last step of the operation is to take the modulo operation by default . Therefore, A+b and a+c in the storage space fixed premise, the final result must be equal.

It has been proved that the subtraction addition is realized by the complement, and the positive number indicates the validity of negative numbers. Let's take a look at the rules for converting the original code to complement.

Suppose the data is stored in n-bit bits, where the leftmost one is the sign bit

modulo = 2n-1 = 1 + 1*2 + 1*22 + ... + 1*2n-2 + 1

A negative source Code a = K0 + k1*2 + k2*22 + ... + kn-2*2n-2

Add equal to modulus according to the absolute value of the mutual complement: if A≡b (mod m), M = |a| + |b| "

A's complement = 2n-1-A = (1-k0) + (1-K1) * + (1-K2) *22 + ... + (1-kn-2) *2n-2 + 1

Since the value of K0,K1 is not 0 or 1, it is equivalent to taking an inverse operation and then adding 1 to the last.

Conclusion

This paper attempts to describe the original code, anti-code and complement in a relatively comprehensive way, if there is a mistake, please correct it.

Respect the original, reproduced please specify from: Fat son John ^_^ http://www.cnblogs.com/fsjohnhuang/p/5060242.html

Thanks

Http://baike.baidu.com/link?url=aGfE7C12rMLNpPVdMZcKlS9JkQnMRe9iiiUUTnQzLSO8PkKE5dbPtf_dtTUwDORVIehhAJ6jr4BK6v0JfyW9l_#4

Http://www.cnblogs.com/zhangziqiu/archive/2011/03/30/ComputerCode.html

Basic field: The original code, the inverse code and the 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.