Some thoughts on the complement

Source: Internet
Author: User
Tags modulus

The advantages of a complement

1, subtraction can be converted to addition, in the computer to keep only the addition

2. Participate in the operation of symbol bit

Second, how to achieve?

We first take the clock as an example, assuming that the standard time is 4 points, and one clock is 7 points in the whole. We can rotate the hour counter counterclockwise by 3, or rotate the hour clockwise by 9 cells.

7-3=7+9=4 MoD (12)

The above equation is a congruence, and the standard of congruence is defined as

A≡b (mod n)

That is, the values on both sides of the same balance are equal to the values of n after the remainder.

From the above example, we can get the idea of replacing negative numbers with a positive number (9 instead of -3 in the example above) to convert subtraction to addition.

In the computer, how should we make this substitution?

We should first clarify some concepts, such as the original code representation, adder overflow, negative residual.

The original code means: the highest bit is the sign bit

Positive number: The sign bit is 0, the remaining value is

If a number is represented by 8 bits, then +8 is 0000 1000 (2)

Negative number: The sign bit is 1 and the remaining value is

If a number is represented by 8 bits, then-8 is 1000 1000 (2)

The use of the original code for computer operations will be very complex, because there will be a lot of logic to judge.

When two numbers are added, the number of lovers is added, the number is subtracted, and the absolute value of the numerical part is compared.

So people found a way to show the complement.

Overflow of adder:

If an adder is a 8-bit adder, when the sum value exceeds 1111 1111 (2), then the high will be discarded.

e.g. 1111 1111 (2) + 1111 1111 (2) = 1111 1110 (2)

In other words, this adder can only represent 0-255, that is, 256 for a cycle , from our point of view, the adder to the results of the remainder , the modulus of 256.

Let's generalize to the general situation, N-bit adder

Xnxn-1......x0, that can only represent 0-2n+1-1, that is, 2n+1 for a cycle , from our point of view, the adder to the results of the remainder , its modulus is 2n+1.

Modulo of negative numbers:

We should first explore the negative number of the modulo operation , we know that the modulus formula is as follows:

X mod y = x-y* [x/y]

Note: [x/y] is the next rounding symbol

Above we say to use positive numbers instead of negative numbers to achieve, the subtraction into addition, then how should be replaced, how is the relationship?

For the general binary number xnxn-1......x0 is discussed, from the above discussion We know that the binary number of the addition and subtraction operation, the final result will be modeled as the 2n+1 of the remainder operation. We assume that the binary value is [x] complement , and that it represents the truth of x.

1, when Xn is 0 o'clock, [x] complement =x, the range is 0-2n-1;

2, when Xn is 1 o'clock, it represents a negative number x, we know here should be a positive [x] complement to replace negative X, and the modulo is 2n+1 equal.

Let us first discuss the -1,-1 of the 2n+1, i.e.

-1 mod 2n+1 = -1-2n+1* ( -1) = 2n+1-1

[2n+1-1] complement = 1

So we use 2n+1-1 instead of-1.

The negative number x is the

X mod 2n+1 = x-2n+1* ( -1) = 2n+1 + x

[2n+1 + x] complement = X

The range of X is 1 to -2n

So we use 2n+1-1 instead of X.

At this point we can draw the formula of the complement:

We also have to prove that it is correct to add in complement.

That we need to prove that

[x] complement + [y] complement = [x+y] complement (mod 2n+1)

There are four things here.

1, x>0, y>0

[x] complement + [y] complement = x + y = [x+y] complement (mod 2n+1)

2, X>0, y<0

[x] complement + [y] complement = x + 2n+1 + y = 2n+1 + x + y = [x+y] complement (mod 2n+1)

3, x<0, y>0

[x] complement + [y] complement = 2n+1 + x + y = 2n+1 + x + y = [x+y] complement (mod 2n+1)

4, X<0, y<0

[x] complement + [y] complement = 2n+1 + x + 2n+1 + y = 2n+1 + 2n+1 + x + y = [x+y] complement (mod 2n+1)

In conclusion, the complement can be converted to addition, and the symbol bit participates in the operation.

Note: From the complement formula can be seen or need to do a subtraction operation, so here with anti-code to do the transition, the original code into the inverse code, and then the anti-code plus 1 to get the complement.

Some thoughts on 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.