>1); return 0; } 为什么输出 -1。 最佳答案 位移仅在无符号类型上定义,对于有符号类-6ren">
gpt4 book ai didi

c - 存储负数

转载 作者:太空狗 更新时间:2023-10-29 16:52:16 24 4
gpt4 key购买 nike

在下面这段代码中

int main()
{
int a = -1;
printf("%d",a>>1);
return 0;
}

为什么输出 -1。

最佳答案

位移仅在无符号类型上定义,对于有符号类型,它是实现定义的。这是 R.. 的有用改进。

Strictly speaking, it is defined for signed types whenever the value is positive and the result does not overflow, and right shift is implementation-defined for negative values. Left shift, on the other hand, is undefined for negative values

┌───┬──────────────┬──────────────────────────────────┬────────────────────────┐
│ │ Unsigned │ Signed, positive │ Signed, negative │
├───┼──────────────┼──────────────────────────────────┼────────────────────────┤
│<< │ well-defined │ well-defined, except on overflow │ undefined behaviour │
│>> │ well-defined │ well-defined │ implementation-defined │
└───┴──────────────┴──────────────────────────────────┴────────────────────────┘

关于c - 存储负数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5476898/

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