简体   繁体   English

在c中添加char值

[英]Addition of char value in c

#include<stdio.h>
int main()
{

  char c =118,a=117;
  c=c+10;
  a=a+10;
  printf("c:%d, a:%d\n", c,a);

}

The answer is c:-128, a:127. 答案是c:-128,a:127。

Can someone explain me why c+10 is -128 and a+10 is 127? 谁能解释我为什么c + 10是-128而a + 10是127?

Thanks in advance. 提前致谢。

Signed 8-bit values range from -128 to 127. The bits for 127 are 0111 1111, and the bits for 128 would be 1000 0000. The problem is, in a signed number, the high order (leftmost) bit is the sign flag (0 is +, 1 is -). 有符号的8位值的范围是-128到127。127的位是0111 1111,而128的位将是10000000。问题是,在有符号数中,高阶(最左边)位是符号标志(0是+,1是-)。 So, because it is signed, the computer interprets this as a negative number with the result -128 (This is called signed overflow, if I remember correctly, and everyone runs into this when programming at one point or another) (check out 2's complement to see why the low 7 bits are 128, not 0). 因此,由于它是带符号的,因此计算机会将其解释为负数,结果为-128(如果我没记错的话,这称为带符号的溢出,每个人在某一点或另一点进行编程时都会遇到此错误)(签出2的补码)以了解为什么低7位是128,而不是0)。 You can get around this "problem" by declaring c and a as unsigned char instead of char . 您可以通过声明C和A为解决这个“问题” unsigned char ,而不是char

BTW, you could save a variable this way: 顺便说一句,您可以通过以下方式保存变量:

char a=117; printf("c:%d, a:%d\\n", a+11,a+10);

117 + 10 = 127 which is in range of the char type ( -128 to 127 ) 117 + 10 = 127 ,在char类型的范围内( -128127

If you open the calculator of your windows in programmer mode, you can see that 118 is represented as 1110110 in binary. 如果以程序员方式打开窗口的计算器,则可以看到118以二进制形式表示为1110110 also, 10 is represented as 1010 . 同样, 10表示为1010 if we add this two, the result is ‭ 10000000‬ . 如果我们增加这两个结果是10000000‬ it is not in the range of the char type, and this number is equivalent to -128 . 它不在char类型的范围内,并且此数字等效于-128 so the -128 is printed. 因此打印-128

because char is 8-bit signed in your compiler. 因为char在编译器中是8位签名的。 So 118+10 is out of range (max is 127 ). 因此118+10超出范围(最大为127 )。

The implementation of your compiler "wraps" it around and you get -128 instead. 编译器的实现将它“包装”起来,您得到-128

In your case, a char is being represented by the compiler on your particular platform as a signed 8-bit value , and is in 2's complement reoresentation . 在您的情况下,在特定平台上,编译器将char表示为带符号的8位值 ,并以2的补码重新表示 That means the highest bit is the sign bit (if it's a 1, then the number is negative). 这意味着最高位是符号位 (如果为1,则数字为负)。 So the range in binary for non-negative values is (in binary) 00000000-01111111 which is 0-127 in decimal. 因此,非负值的二进制范围是(二进制)00000000-01111111(十进制为0-127)。 The negative values range (in binary) from 10000000 to 11111111 which is -128 to -1 in decimal. 负值范围(以二进制表示)从10000000到11111111,十进制为-128到-1。

If you start with 118, in binary that's: 如果您以118开头,则为二进制:

01110110

If I add decimal 10, that's adding 1010 in binary: 如果我添加十进制10,则二进制添加1010:

 01110110
+00001010
---------
 10000000

You can see now the highest bit is set, meaning the number overflowed (became greater than the maximum 127 in decimal) and now represents a negative number. 您现在可以看到设置了最高位,这意味着溢出的数字(大于十进制的最大值127)现在代表一个负数。 The 8-bit binary value 10000000 happens to represent -128 in decimal. 8位二进制值10000000恰好代表十进制的-128。

So adding (in decimal) 10 to the char value 118, yields -128. 因此,在char值118上加上(十进制)10,得到-128。

Your value of a is 117, so 10+117 = 127 still fits in the 7 bits for a positive value of 127. You can do the above binary analysis as an exercise to see how that works. 您的a值为117,所以10 + 117 = 127仍适合7位,而正值为127。您可以进行上述二进制分析作为练习,以了解其工作原理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM