位用C移一个int 32倍 [英] Bit shifting an int 32 times in C

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

问题描述

我有一个特定的C位移场景,我不相信覆盖堆栈溢出呢。 (如果是,我一直没能找到它!)

I have a specific C bit-shifting scenario that I do not believe is covered on Stack Overflow yet. (If it is, I haven't been able to find it!)

本练习中使用符号int S作为数据类型。

This exercise is using signed ints as the data type.

取值为0x80000000 (这只是最显著位1)。

Take the value 0x80000000 (which is just a 1 in the most significant bit.)

使用算术右移(其中矿一样)移位它的权利,一旦一台计算机上。

Shift it right once on a machine using arithmetic right-shifts (which mine does).

结果= 0xC0000000的(最左边在1100字节0000)。

Result = 0xC0000000 (1100 0000 in leftmost byte).

继续转向它,你应该填补了的人,从左边到右边。

Continue shifting it and you should be filling up with ones, from the left to the right.

结果= 0xFFFFFFFF的(所有的人。)

Result = 0xFFFFFFFF (All ones.)

但是:尝试相同的例子,但转向一个额外的位置,都在一起:

However: Try the same example but shift one extra position, all together:

0x80000000的>> 0x00000020 (右移32倍)

您的结果?我不知道。我的结果是不是所有的人。事实上,我得到了 00000001 这不是我所期望的行为。为什么是这样?它是该机的具体?

Your result? I don't know. My result was not all ones. In fact, I got 0x00000001 which was NOT my desired behavior. Why is this? Is it machine specific?

(语境。家庭作业限制我的行动,一些运营商一点解决一个谜这是一个谜的一个方面,但远不是全部问题)

推荐答案

这是不确定的行为

<一个href=\"http://stackoverflow.com/questions/7401888/why-doesnt-left-bit-shift-for-32-bit-integers-work-as-expected-when-used\">Why不留位移,与QUOT;&LT;&LT;&QUOT ;,为32位整数按预期使用超过32倍时

这意味着你应该避免像瘟疫。下面是一些链接,包括每一个C程序员应该知道的未定义行为是什么: http://lwn.net/Articles/ 511767 /

which means you should avoid it like the plague. Here's some links including "What every C programmer should know about undefined behavior": http://lwn.net/Articles/511767/

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

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