两周前改了模型关于字符串拼接的实现,为了达到任意位置拼接的效果。其中有段代码简化如下:
unsigned int a = 0x1234;
unsigned int addr = 0x48;
a = a<<((4-addr%4)*8); //在根据地址计算后的表达式为 a = a <<32;
我希望得到a = 0的结果,结果得到的是a= 0x1234的输出。在通过简化后的结果校验时,在windows下编译是有Warning提示:
C4239 'operator': shift count negative or too big, undefined behavior
If a shift count is negative or too large, the behavior of the resulting image is undefined.
代码修改成
unsigned int a = 0x1234;
unsigned int addr = 0x48;
a =(unsigned int) (((unsigned long long)a)<<((4-addr%4)*8)&0xffffffff); //在根据地址计算后的表达式为 a = a <<32;