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