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位,我需要正确处理它。 –