Use the unsinged type as operands of shift operator in C language

K&R中如是说:

Right shifting a signed quantity will fill with bit signs (``arithmetic shift'') on some machines and with 0-bits (``logical shift'') on others.

也就说对于负数的右移位,首位补的1 or 0是实现相关的。

我们再来看C99标准:

Image(2)

简而言之,

对于<<, 如果左操作数是signed type, 负数是undefined behavior, 正数如果移位溢出后也是undefined behavior

对于>>, 如果左操作数是singed type, 负数的话其值是implementation-defined.

对于C++2003标准

Image(3)

简而言之,

对于<<, 标准似乎没有定义对于有符号数的情况。

对于>>,标准定义了负数的话,是一个implementation-defined

原文链接: https://www.cnblogs.com/aoaoblogs/archive/2010/12/27/1918354.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    Use the unsinged type as operands of shift operator in C language

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/19314

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月7日 下午8:22
下一篇 2023年2月7日 下午8:22

相关推荐