在C位的变化 [英] Bit shifts in C

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

问题描述

如果对应于一个符号的整数的位模式被移位到右侧然后

If the bit pattern corresponding to a signed integer is shifted to the right then

1   vacant bit will be filled by the sign bit	
2   vacant bit will be filled by 0	
3   The outcome is implementation dependent	
4   none of the above

在这个问题的答案是第三选择..任何人都可以解释这个,,

The answer to this question is 3rd option.. Can anybody explain this,,

也给一些基本思想,关于C编程左移与右移运营商背后的理论。例如。

Also give some basic idea, about the theory behind left shift and right shift operators in C programming. E.g.

什么在执行任何操作时,被填充在空位。我查了一下,发现左移由0填补空缺位和右移1。填写,请清除逻辑...

what is filled on the vacant bit when any of the operation is performed. I checked and noticed that left shifting fills the vacant bit by 0 and right shift fills by 1. Please clear the logic...

推荐答案

我要检查的规范是什么precisely问题是实现有关。
然而,每一个实现我(喃喃)年嵌入式系统项目已经使用已经懂事:

I'd have to check the spec for the question of what precisely is implementation dependent. However, every implementation I've used in (mumble) years of embedded systems projects has been sensible:


  • 左移始终处于低位的0移位。没有其他的价值是有道理的。

  • Left shifts always shift in a 0 at the low bit. No other value makes sense.

右移取决于数据类型。有符号整数右移复制高位因为它转移休息的权利。这就是所谓的算术移位,并有不错的属性(在二进制补码算术,至少),它由两部分价值,同时preserving原数字的符号。

Right shifts depend on the data type. A right shift of a signed integer duplicates the high bit as it shifts the rest to the right. This is called an "arithmetic shift", and has the nice property (in twos complement arithmetic, at least) that it divides the value by two while preserving the sign of the original number.

无符号整数右移一个转移到0的高位,通常被称为逻辑移位。

A right shift of an unsigned integer shifts a 0 into the high bit, and is usually known as a "logical shift".

是有道理的实现提供两种班次因为两者都是有用的,并且使用符号/无符号,选择这意味着是一个明智的选择。

It makes sense for an implementation to provide both kinds of shifts because both are useful, and using signed/unsigned to select which is meant is a sensible choice.

编辑:至少有一点是绝对依赖于实现的是C标准不(完全)指定整数及其存储的底层实现。例如,它可能建立一个兼容C编译器使用的补算术的机器。这也将是可能的(我认为)建立一个兼容的编译器一台机器的本机存储签署幅度BCD。 (不,我错了,请参阅下文。)

At least one thing that absolutely is implementation dependent is that the C standard does not (completely) specify the underlying implementation of integers and their storage. For instance, it is possible to build a compliant C compiler for a machine that uses one's complement arithmetic. It would also be possible (I think) to build a compliant compiler for a machine whose native storage was signed magnitude BCD. (Nope, I was wrong, see below.)

在实践中,世界是pretty上的二进制补码CPU和一些迂腐的是酝酿多结算。

In practice, the world is pretty much settled on two's complement binary for the CPU and some of the pedantry is mooted.

所以,问题的一部分真的是:你怎么定义的&lt的含义;<而>>运算符的方式,无论使用的底层算法系统是稳定的。

So part of the question really is: how do you define the meaning of the << and >> operators in a way that is stable regardless of the underlying arithmetic system used.

IIRC,定义 N'LT;&LT; 1 实际上是 N * 2 N'GT;&GT; 1 是有效 N / 2 ,有超过1(但不超过31的自然延伸转变。 ..有未定义龙那里...),并与概念,即&GT;方式&gt; 运营商将preserve标志,如果在一个符号值操作

IIRC, the definition of n<<1 is effectively n*2, and n>>1 is effectively n/2, with a natural extension to shifts by more than 1 (but not more than 31... there be undefined dragons there...) and with the notion that the >> operator will preserve the sign if operating on a signed value.

编辑2:皮特Kirkham的指出了他美好的答案,在C标准没有明文禁止整数的BCD重新presentation的可怕情况下,无论是签署幅度或十年的补。我敢肯定这是一件好事,即使克努特确实使用(可选)BCD机中的计算机编程的艺术。

Edit 2: Pete Kirkham points out in his fine answer that the C standard does specifically disallow the scary case of a BCD representation of integers, whether it is signed magnitude or ten's complement. I'm sure that is a good thing, even if Knuth did use a (optionally) BCD machine for his sample code in early editions of The Art of Computer Programming.

在罕见的使用情况下,BCD是正确的答案,然后将它们存储在一个unsigned long(8位十位补数)或64位无符号整数(室16位十位补码或15位加符号及标志)并使用一个精心制作的运算库操纵它们是有道理的。

In those rare use cases where BCD is the right answer, then storing them in an unsigned long (8 digits ten's complement) or an unsigned 64-bit integer (room for 16 digits ten's complement or 15 digits plus sign and flags) and using a carefully crafted arithmetic library to manipulate them makes sense.

在实践中,当然,C实现的运营商为直接映射所允许的标准的CPU的本机机器指令。谁写的标准的民歌是的非常的意识到存在的实现,甚至简单的事情,就像一个积分值的重新presentation多种方式,和C标准的反映,通过允许刚够实现定义的行为让运营商在每一台机器被有效执行。

In practice, of course, C implementations map the operators as directly as allowed by the standard to the native machine instructions of the CPU. The folk who wrote the standard were very mindful of the existence of of many ways to implement even simple things like the representation of an integral value, and the C standard reflects that by allowing just enough implementation defined behavior to let operators be efficiently implemented in each machine.

替代迅速导致其中所有数学运算被完全指定的世界,并不能在任何机器上被有效地实现。

The alternative leads swiftly to a world where all math operations are completely specified, and cannot be efficiently implemented on any machine.

这篇关于在C位的变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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