为什么不是C已转左/右运营商? [英] Why doesn't C have rotate left/right operators?

查看:176
本文介绍了为什么不是C已转左/右运营商?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个哲学问题的一点,我想。

A bit of a philosophical question, I suppose.

C语言有标准的逐位操作,包括 XOR SHIFT左/右不是。为什么不向左旋转/旋转包含在语言正确的运算符或函数?

The C language has the standard set of bit-wise operations, including OR, AND, XOR, SHIFT LEFT/RIGHT, and NOT. Why aren't rotate left/rotate right operators or functions included in the language?

这些运营商都是一样的复杂性等位运算符和通常需要一个单一的汇编指令,和其他人一样。此外,我能想到很多关于旋转操作人员使用的,大概不会比说, XOR 运营商少 - 所以它听起来有点怪怪的,我认为他们不是列入下与其他地方一样。

These operators are of the same complexity as other bit-wise operators and normally require a single assembly instruction, like the others. Besides, I can think of a lot of uses for rotate operator, probably not less than, say, xor operator - so it sounds a bit strange to me that they aren't included in C along with the rest.

如果你确实需要用C或C ++旋转,<一个href=\"http://stackoverflow.com/questions/776508/best-practices-for-circular-shift-rotate-operations-in-c\">there's有关最佳实践为它的一个单独的FAQ问题。的讨论是这个问题题外话。

If you do need to rotate in C or C++, there's a separate FAQ question about best-practices for it. Discussion of that is off-topic for this question.

推荐答案

我想这是因为有两种类型的旋转:有和没有携带,这使得旋转而做不同,根据所产生的机床的进位标志( 1或0)。这将意味着执行总共4运营商的,从而使语言不必要的复杂,条件是旋转可以作为@Aniket表明被简单地实现。

I think it's because there are two types of rotations: with and without carry, which makes the rotation to be done differently, according to the resulting machine's CARRY flag (1 or 0). That would imply implementing a total of 4 operators, thus making the language unnecessarily complex, provided that rotation can be simply implemented as @Aniket has shown.

修改

不过,换挡也可以做符号和无符号。其实JavaScript有两个运营商,据我所知。然而,由于C支持符号和无符号变量,我认为它没有意义执行轮班签约,因为编译器应该知​​道我们是否正在把一个符号或无符号的变量。符号/无符号的变化是算术计算是有用的,和C编译器可以使用它们来生成汇编code。例如,许多诸如乘以或由2的幂除算术运算是由编译器到移位操作转换。我们必须转变运营商在C中的唯一原因是与位掩码的工作。

Nevertheless, shifting may also be done signed and unsigned. Actually Javascript has both operators, AFAIK. However, since C supports signed and unsigned variables, I think it has no sense to perform signed shifts, because the compiler should know whether we are shifting a signed or unsigned variable. Signed/unsigned shifts are useful for arithmetical computations, and a C compiler may use them to generate the assembly code. For instance, many arithmetical operations such as multiplying or dividing by a power of 2 are translated by the compiler into shift operations. The only reason we have shift operators in C is for working with bitmasks.

这篇关于为什么不是C已转左/右运营商?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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