C Language base of "original code, counter code, complement"

Source: Internet
Author: User
About the original code, the inverse code, the complement we must first clear:

only the symbolic number has the original code inverse code, which is stored in the form of complement in memory, unsigned numbers are stored as binary numbers in memory.

The so-called signed number of the original code is its highest bit as a sign bit, 0: positive, 1 for negative. The remaining numeric digits represent the size of the value. A positive number has the same original code counter code. The original code of negative numbers is equal to the original code of the absolute number except the sign bit. The inverse of a negative number is equal to the reverse of its original code in addition to the sign bit. The complement of a negative number is added 1 on the basis of its inverse code.

Let's look at two simple examples:

(1)

int main (int argc, char **argv)
{
unsigned char c;

c = 300;
fprintf (stdout, "C =%d/n", c) p;
return 0;
}

Result: C = 44;

This is well understood because the data overflow 300-256 is just 44;

(2)

int main (int argc, char **argv)
{
Signed Char c;

c = 200;
fprintf (stdout, "C =%d/n", c);
return 0;
}

The result is: c =-56;

Why not: 200-128 equals 72.

But-56?

This is because 200 is stored in memory in the form of 11001000, whether it is unsigned or signed.

c = 200, or 11001000 writes in the byte represented by the variable C. Because we have declared C to be a signed number. Symbols are stored in the form of complement in memory. So the compiler sees 11001000 as the complement. Minus 1 Gets the counter code 11000111. In addition to the symbol bit, you get the original code 10111000. Convert to decimal exactly bit-65.

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.