C语言unsigned a = -10.也对啊不是无符号数吗?该怎么解决
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.****.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 在计算机眼里就是 个补码,它不管正数负数
------解决方案--------------------
“-10”是int类型的。存在int到unsigned int的标准转换。所以没有问题。你还可以用'X'来替代-10呢。
------解决方案--------------------
------解决方案--------------------
忽略最高位。在32bit计算机中这会是个很大的数。
------解决方案--------------------
先把-10转换为无符号整形,相当于-10+MAX_INT
------解决方案--------------------
------解决方案--------------------
一个无符号数被什么的数赋值,C语言不会管你的
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.****.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 在计算机眼里就是 个补码,它不管正数负数
------解决方案--------------------
“-10”是int类型的。存在int到unsigned int的标准转换。所以没有问题。你还可以用'X'来替代-10呢。
------解决方案--------------------
------解决方案--------------------
忽略最高位。在32bit计算机中这会是个很大的数。
------解决方案--------------------
先把-10转换为无符号整形,相当于-10+MAX_INT
------解决方案--------------------
------解决方案--------------------
一个无符号数被什么的数赋值,C语言不会管你的