如果我们按位移位以上的整数其大小会发生什么 [英] What happens if we bitwise shift an integer more than its size

查看:111
本文介绍了如果我们按位移位以上的整数其大小会发生什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的C code Visual Studio中编译,结果是4。

On Visual Studio compiling following C code , the result is 4 .

void main() { int c = 1; c = c<<34;}

从上Visual Studio的反汇编窗口看到的组装code是

The assembly code as seen from on Visual Studio disassembly window is

shl eax,22h

在汇编,它很容易看到,我们正在改变34.由于整数这里是4个字节,从结果来看,显然模操作进行机器级别2,使这项工作作为转变。

From assembly , its easy to see that we are shifting 34. Since the integer here is 4 bytes , from the results it is obvious that modulo operation was carried out on machine level to make this work as shift by 2.

我在想,如果这种行为是跨平台标准化和跨平台的变化?

I was wondering if this behavior is standardized across platforms or varies across platforms?

推荐答案

未定义的行为发生。这是由语言标准规范(见1999年部分 6.5.7按位移C标准的运营商)。 UB的观测效应不规范,但是,可能会有所不同。

Undefined behavior happens. That's standardized by the language standard (See section 6.5.7 Bitwise shift operators of the C standard from 1999). The observable effects of UB are not standardized, however, and may vary.

至于 SHL EAX,22小时,位移计数要由CPU截断为5位(请参阅文档),这说明真的会表现为 SHL EAX,2

As for shl eax, 22h, the shift count is going to be truncated to 5 bits by the CPU (see the documentation) and this instruction will really behave as shl eax, 2.

这篇关于如果我们按位移位以上的整数其大小会发生什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆