你能告诉我为什么下面的代码由于无符号数的左移而给出负结果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/