Explain why a signed char can indicate a range of-128 ~ + 127, char-128

Source: Internet
Author: User

Explain why a signed char can indicate a range of-128 ~ + 127, char-128

Q: Why does a signed char indicate a range of-128 ~ + 127?

To understand this problem, you must first understand the following points:

When the computer knows these rules, it can be converted perfectly. Let's look at how the computer converts:

First, the 8-bit change range in the computer is:

The complement code in the computer, the converted original code, and the decimal number actually represented by the original code.

0000 0000 0000 0000 0 0

.................

0111 1111 0111 1111 127 127

1000 0000 1000 0000-128

...................

1111 1111 0000 0001 1-1

I can see how the 128 is changed to-128, and how the 1 is changed to-1 at a time. Then I can go back and check the third rule, the computer already knows that the eight bits store a negative number. Do not impose the symbol bit on yourself. It is a recognition symbol for the computer, the computer uses this identification label to know whether it stores positive numbers or negative numbers, which does not affect the value on this bit. Therefore, the calculated 128 value is negative, therefore, we can see from the above derivation that the range is-128 to 127.

Two exercise questions about complement:

What is the final output result of the main function?

 1 int main(void) 2 { 3     char a[1000]; 4     int i=0; 5     for(i=0;i<1000;i++) 6     { 7         a[i]=-1-i; 8     } 9     printf("%d\n",strlen(a));10     return 0;11 }

Result: 255

Q: What is the output result of the foo function?

1 void foo()2 {3     unsigned int a=6;4     int b=-20;5     (a+b)>6?puts(">6"):puts("<=6");6     printf("%u\n",a+b);7 }

Result> 6

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.