C语言unsigned a = -10.也对啊?不是无符号数吗?
C语言unsigned a = -10.也对啊?不是无符号数吗? 求解释。谢啦
[解决办法]
先把-10的补码转换成无符号数,赋给a
[解决办法]
ISO/IEC 9899:1999 (E)
6.2.5 Types
9 The range of nonnegative values of a signed integer type is a subrange of the
corresponding unsigned integer type, and the representation of the same value in each type is the same.31) A computation involving unsigned operands can never overflow,because a result that cannot be represented by the resulting unsigned integer type is reduced modulo the number that is one greater than the largest value that can be represented by the resulting type.
[解决办法]
直接补码了吧 具体可以看看楼上貌似是高人
[解决办法]
把 负号 直接当成数字运算了
这就是C……这种事情不报错……
[解决办法]
可以参考下:http://blog.csdn.net/lida2003/article/details/6973469
[解决办法]
不会出错,
结果就是-10对2的32次取模:
a = -10 % (2^32);
a = 4294967286;
<<C++ Primer》第二章有介绍
[解决办法]
unsigned char a = -1;
a就会等于 -1 % (2^8) = -1 % 256 = 255;
[解决办法]
计算机看来的话负数对应的是一个大的正数,给无符号赋一个负值,顶多编译器会给个警告,相当于进行强制转换。
[解决办法]
-10 在计算机眼里就是 个补码,它不管正数负数