Read why a signed char can represent a range of -128~+127

Source: Internet
Author: User

Q: Why is a signed char representing a range of -128~+127?

To understand this problem, first understand the points:

    1. The complement exists for char and int computers.

    2. Strictly speaking, the computer is stupid, it only knows whether a bit is 0 or 1.

    3. We are telling the computer a rule: if these 8 bits are 0, then you have a positive number stored in the 8 bits, and if the 8 bits are 1, then you are storing a negative number in the 8 bits.

    4. We also tell the computer a rule: you this idiot, you store the complement is a what thing, I do not understand, you want to humanized a little, put this thing to me into source code, if use decimal show out that the best, So you tell the computer how to convert the number of the 8 digits to the result you want: if you have 8 bits of storage is a positive, then the source code and the complement is the same, you put the 8 bits stored in the 2 binary conversion 10 conversion is good, if you 8 bits of storage is a negative, Then you need to give this 8 digits plus 1, and then reverse, that is, the source code, and then converted into 10 binary is good.

When the computer knows the rules and can transform them perfectly, let's see how the computer is transformed:

The first 8-bit range of changes in the computer is:

The complement of the computer we converted the original code we get from the original code decimal number actually represented by the decimal

0000 0000          0000 0000               0                                              0

.....                         ....                            ....                                              &NBSP, .....

0111 1111 0111 1111 127 127

1000 0000 1000) 0000 128-128

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

1111 1111 0000) 0001 1-1

See the 128 how it became-128, and the 1 how suddenly became-1, and then go back to see the third rule will know that the computer has known it is a negative in the 8-bit, do not impose the concept of the symbol bit, it is to the computer a recognition mark, The computer is identified by this identification that it is stored in a positive or negative, does not affect the value we use this bit, so we calculated this 128, it is a negative, so the above deduction can be seen from the range is 128 to 127

Two exercises on the complement:

Q. What is the final output of the main function?

1 intMainvoid)2 {3     Chara[ +];4     intI=0;5      for(i=0;i< +; i++)6     {7a[i]=-1-i;8     }9printf"%d\n", strlen (a));Ten     return 0; One}

The result is: 255

Q What is the output of the Foo function?

1 voidfoo ()2 {3UnsignedintA=6;4     intb=- -;5(a+b) >6? puts (">6"):p UTS ("<=6");6printf"%u\n", A +b);7}

The result is >6.

Read why a signed char can represent a range of -128~+127

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.