A deep understanding of complement in C Language Learning

Source: Internet
Author: User
Tags modulus


When learning the essence of C language complement code (http://learn.akae.cn/media/ch14s03.html ). It is not very understandable, especially the description section.

If 8 bits use the 2's sComplement notation,

The value range of negative numbers is from 10000000 to 11111111 (-128 ~ -1 ),

Positive numbers are from 00000000 to 01111111 (0 ~ 127 ).

So I searched a lot of materials and finally clarified this point.

First, the original code, the anticode, And the complement code are different methods in which the true value is expressed in binary. The process of displaying a truth table as a machine number of binary strings is called encoding. The unsigned number does not contain the original code, reverse code, or complement code. Different encoding methods are available only for the number of bits.

Why does the computer end up with a supplementary code? For details, see here

Http://sharecourse.upln.cn/courses/c_809_04/theory/module_2/pdf/whybm.pdf

You can also read the above article by converting the original code into a complement code.

One important thing here is how to convert from a complement code to an original code or a true value?

In fact, this process is the same as converting the original code into a complement code. This is not mentioned in many learning materials. I am in

Http://wenwen.soso.com/z/q110390792.htmhere.

With the above knowledge, Let's explain what the scope of the code is.

1 first, from the simple start, the positive number range, the binary range of the obvious complement code is from 00000000 to 01111111. Take a few special values for a look,

0000,0000

0111,1111

Who is the complement of the two binary numbers?

That is, what is the true value of (00?

(0111,1111) What is the true value?

Because the positive complement is the same as the true value,

Therefore, the true value (127) is 0, and the true value () is.

It can be seen that a positive number is expressed by a complement code, and the value range is 0-127.

2. The range of negative numbers. The binary range of the obvious complement code is from 10000000 to 11111111. Take a few more special values to see,

So what is the true value of 10000000?

What is the true value of 10000001?

What is the true value of 11111111?

The above describes how to convert from complement code to true value.

After calculation, it is found that

The true value of 10000000 is (-128,) =-

The true value of 10000001 is (-127) =-

The true value of 11111111 is (-,) =-1.

Therefore, a negative number is indicated by a complement code, and the range is-128 to-1.

Here, let's calculate it. (Ignore the sign bit first)

10000001 (this is a supplemental code)

Get (111,1110)

+ 1, get (111,1111)

Add the symbol (-111,1111), that is,-127

() (This is a supplemental code)

Get (111,1111)

+ 1, get)

Add the symbol bits to-128, that is.

Then let's verify if the complement form of the true value-127 is ).

-127 This is the true value (-111,1111)

Convert to original code (1111,1111)

Convert to reverse code (,) (the symbol bit remains unchanged)

+ 1, () (the symbol bit remains unchanged)

Verify whether the complement format of the true value-128 is ).

-128 (-,) indicates that the original code cannot be expressed.

Only in this way,

First, regardless of the symbol bit, get ).

Obtain the inverse value)

+ 1, get (), 7 bits overflow, remove carry)

Complete the signed bits ).

In fact, we only need to remember that the complement form of the true value-128 is.

In fact, the method of anti-increment 1 is flawed, not the definition of the complement code. Only by understanding the definition of the complement code can we truly understand the conversion between the complement code and the true value.

Is to use the same remainder. Http://sharecourse.upln.cn/courses/c_809_04/theory/module_2/pdf/whybm.pdf

In mathematics, we use the concept of "same remainder" to describe the above relationship, that is, two integers A and B use the same positive integer M (M is called

Modulo) is removed while the remainder is equal, it is called A and B to M with the remainder, recorded:

A = B (MOD M)

There is a complementary relationship between the two numbers that have the same remainder, one of which is called the complement code of the other. When M = 12 ,-

5 and + 7,-4 and + 8,-3 and + 9 are the same, and they complement each other.

From the concept of the same remainder and the example of the above clock, it is not difficult to draw a conclusion: for a certain modulus, subtract a certain number.

A number smaller than the modulo can always be replaced by the difference between the modulo and the absolute value of the number. Therefore

In a modulo operation, subtraction can be done as an addition.

We can find that there is a skill that the sum of the two numbers that complement each other is just equal to the modulo.

For the 8-bit binary number, the modulo is equal to the 8th power of 2 = 256.

Who is the complement of true-127?

-127 is equivalent to adding a modulus minus 127, that is, adding 129 ).

Therefore,-127 and 129 (,) complement each other.

That is, the complement code of true-127 is (,). This complement code looks positive and actually represents a negative number.

Now let's take a look at whether the complement format of true-128 is ).

Apparently,-128 is equivalent to adding 256-128 = 128, that is ).

What is the complement of true-126?

-126 is equivalent to adding 256-126 = 130, that is ).

The above is a complement obtained from the true value.

So how can we get the value from the complement code?

For example, what is the true value of the complement code?

Obviously (130,) =, the symbol bit is 1, indicating that the true value is negative,

130-256 =-126.

What is the true value of the complement code?

Obviously, (128) =, the symbol bit is 1, indicating that the true value is negative,

128-256 =-128.


The night is deep. First, we will understand this step, and then summarize and update it later.


Related Article

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.