Current location - Training Enrollment Network - Mathematics courses - Understand the original code, complement and complement.
Understand the original code, complement and complement.
It should be declared that the sum of numbers and operations involved in this paper are based on values below 8 bits.

The most significant bit is the sign bit, 0 represents a positive number, 1 represents a negative number, and the unsigned bit is the binary representation of the absolute value of the number.

For example:

The original code of 127 is 01111.

-127 The original code was1111.

The negative code of positive number is consistent with the original code;

The negation of a negative number is the bitwise negation of the original code, but the highest bit (sign bit) remains unchanged.

For example:

The anti-code of 127 is 01111.

The inverse code of-127 is 1000 0000.

The complement of positive number is consistent with the original code;

The complement of a negative number is the complement of the number plus 1.

For example:

The complement of 127 is 01111.

The complement of-127 is 1000 00 1.

To sum up:

Let's discuss why we should use complement to represent numbers.

If the number is represented by the original code in the computer, it needs to be converted into the addition and subtraction of two absolute values when adding and subtracting;

It's a bit expensive for a computer to realize both adder and subtractor at the same time, so can the remote calculation of addition and subtraction be realized with only one type of operator?

It's easy to think of reduction as addition. Let's take a life example to illustrate this problem:

A circle of the clock is 360 degrees. Of course it's 365 degrees, but it's the same as 5 degrees.

Similarly, -30 degrees means 30 degrees counterclockwise, which is the same as 330 degrees clockwise;

Here, the number 360 represents one turn of the clock. In the computer, a similar concept is called module, which can be simplified to addition, essentially discarding the overflow part without changing the result.

Easy to get, the modulus of single byte (8 bits) operation is 256 = 2 8.

If there is no sign bit, 127+2= 129, that is:

At this time, we take most significant bit as the sign bit, and the number of computers is represented by its complement, so the original code of 100000 1111165438.

That is to say, 129 means-127 in the computer, which is equivalent to one turn of the module 256. Clockwise 129 is the same as counterclockwise 127, that is-127.

Therefore, the following conclusions can be drawn:

The complement of a negative number is the norm minus the absolute value of the number.

For example, the complement of -5 is:

-5 = 256-5 = 251=111(binary)

Similarly, the critical value-128 can also be expressed as:

-128 = 256-128 =128 =100000 (binary)

But the positive number 128 will overflow, so the number range represented by a single byte (8 bits) is-128- 127.

Finally, let's take a look at how complement is simplified to addition through module overflow and discard operations!

16-5= 16+(-5)= 1 1

100001010000101/kloc if the overflow bit is discarded.

Ok, this article is shared here. I hope I can help you.