由32位移动一个32位整数 [英] Shifting a 32 bit integer by 32 bits

查看:186
本文介绍了由32位移动一个32位整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我吊索一些C code,我需要bitshift一个32位的int左边32位。当我运行这个code与参数n = 0,换挡不会发生。

I'm slinging some C code and I need to bitshift a 32 bit int left 32 bits. When I run this code with the parameter n = 0, the shifting doesn't happen.

int x = 0xFFFFFFFF;
int y = x << (32 - n);

为什么不这项工作?

Why doesn't this work?

推荐答案

在转移自己的危险。按照标准,你想要做的就是未定义行为

Shift at your own peril. Per the standard, what you want to do is undefined behavior.

C99§6.5.7

3 - 整数促销活动在每个操作数执行。该结果的类型是,促进左操作数。如果右操作数的值是负的或大于或等于促进左操作数的宽度,其行为是不确定的。

3 - The integer promotions are performed on each of the operands. The type of the result is that of the promoted left operand. If the value of the right operand is negative or is greater than or equal to the width of the promoted left operand, the behavior is undefined.

在换句话说,如果您尝试通过任何一个32位的值移位超过31位,或者负数,你的结果是不确定的。

In other words, if you try to shift a 32bit value by anything more than 31 bits, or a negative number, you're results are undefined.

这篇关于由32位移动一个32位整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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