gpt4 book ai didi

无法理解c中负数的右移运算符运算

转载 作者:行者123 更新时间:2023-11-30 15:04:19 25 4
gpt4 key购买 nike

我有这段简单的代码。谁能解释为什么输出是 ffff不是0fff ??

#include<stdio.h>

int main(){

printf("\n%x",(-1>>4));

return 1;
}

最佳答案

最好避免移动负数。对于 <<这是未定义的行为。对于 >>它是实现定义的:

The result of E1 >> E2 is E1 right-shifted E2 bit positions. If E1 has an unsigned type or if E1 has a signed type and a nonnegative value, the value of the result is the integral part of the quotient of E1 / 2E2. If E1 has a signed type and a negative value, the resulting value is implementation-defined.

关于无法理解c中负数的右移运算符运算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40329002/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com