c - 如果将正数左移为负值,为什么会得到负结果?

标签 c binary bit-shift

你能告诉我为什么下面的代码由于无符号数的左移而给出负结果a具有负值?

    int main(void)
     {
          unsigned int a=1;
          printf("%d",a<<-1);   

     }

输出 -2147483648。

最佳答案

来自this article :

If the value of the right operand is negative or is greater than or equal to the width of the promoted left operand, the behavior is undefined.

所以你所做的是未定义的行为。如果您想要可预测的行为,请不要移动负数的位数。

关于c - 如果将正数左移为负值,为什么会得到负结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16413267/

相关文章:

c - fgets() 从键盘读取新行字符

java - Java 和 C 之间传递值

c - 如何从c中的void函数返回动态数组?

JAVA猜数游戏使用方法: method for play again does not work properly

c - K&R - 理解练习 2-8 : Exactly what is asked here?

c++ - 什么时候右移操作>>移位符号位,什么时候不移位?

c - 如何将参数传递给 makefile

c - 找不到 Eclipse mac osx : Launch failed, 二进制文件

mysql - 在 MySQL 中存储 BINARY 的优点

c++ - 使用左移运算符将 F 转换为 FF