int左移32位的行为未定义/Coverity

本文讨论了在x86架构下将uint64_t类型的整数左移超过32位时可能出现的未定义行为。文章指出,如果直接将32位内的数值进行移位,则该操作将引发未定义行为。通常情况下,移位结果可能是移位距离模32或0,具体取决于硬件如何处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

int左移32位的行为未定义

Coverity 代码静态安全检测

Is Shifting more than 32 bits of a uint64_t integer on an x86 machine Undefined Behavior?

 

uint64_t s = 1 << 32;

uint64_t s = 1ULL << 32;

Note, however, that if you write a literal that fits into 32 bits, e.g. uint64_t s = 1 << 32 as surmised by @drhirsch, 
you don't actually shift a 64-bit value but a 32-bit one. That is undefined behaviour. The most common results are a
shift by shift_distance % 32 or 0, depending on what the hardware does.

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值