32位型计算机,C++ - 警告:右移位计数型的> =宽度32位计算机上

void func(unsigned long v)

{

char max_byte = 0xFF;

char buffer[8];

buffer[0] = static_cast((v) & max_byte);

buffer[1] = static_cast((v >> 8) & max_byte);

buffer[2] = static_cast((v >> 16) & max_byte);

buffer[3] = static_cast((v >> 24) & max_byte);

buffer[4] = static_cast((v >> 32) & max_byte);

buffer[5] = static_cast((v >> 40) & max_byte);

buffer[6] = static_cast((v >> 48) & max_byte);

buffer[7] = static_cast((v >> 56) & max_byte);

}

它接受一个unsigned long参数,并插入其8个字节到char缓冲液(不尝试找出原因,这是一个有意义功能的简洁版本)。

此代码编译以及在64位,但在32位,我得到以下警告:

warning: right shift count >= width of type

参考线:

buffer[4] = static_cast((v >> 32) & max_byte);

buffer[5] = static_cast((v >> 40) & max_byte);

buffer[6] = static_cast((v >> 48) & max_byte);

buffer[7] = static_cast((v >> 56) & max_byte);

我想我明白了警告,但我不当然我该怎么做才能在32位上顺利编译它。

2014-05-09

idanshmu

+4

是的,它是[未定义的行为](http://stackoverflow.com/questions/18918256/is-right-shift-undefined-behavior-if-the-count-is-larger-than-the -width-of-the-t/18918340#18918340)执行宽度大于或等于位数的移位。 –

+0

32位系统上'unsigned long'的长度是多少? –

+0

@ShafikYaghmour我明白,但解决方案是什么。 'unsigned long'的长度是64位,我需要正确处理它。 –

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值