简体   繁体   English

C#中的“>>”运算符有什么作用?

[英]What does the “>>” operator in C# do?

I ran into this statement in a piece of code: 我在一段代码中遇到了这个声明:

Int32 medianIndex = colorList.Count >> 1;

colorList is a list of class System.Drawing.Color . colorListSystem.Drawing.Color类的列表。

Now the statement is supposed to retrieve the median index of the list .. like the half point of it .. but I can't understand how that >> symbol works and how the "1" is supposed to give the median index .. I would appreciate some help :S 现在声明应该检索列表的中间索引..就像它的半点..但我无法理解>>符号是如何工作的以及“1”应该如何给出中间索引。我会感激一些帮助:S

The >> operator performs a bit shift . >>运算符执行位移

The expression >> 1 is almost* the same as / 2 so the programmer was calculating the index colorList.Count / 2 which is** the median . 表达式>> 1几乎与/ 2相同,因此程序员正在计算索引colorList.Count / 2 ,即**中位数 To understand why this is the case you need to look at the binary representation of the numbers involved. 要理解为什么会出现这种情况,您需要查看所涉及数字的二进制表示。 For example if you have 25 elements in your list: 例如,如果列表中有25个元素:

n     : 0 0 0 1 1 0 0 1 = 25
         \ \ \ \ \ \ \
n >> 1: 0 0 0 0 1 1 0 0 = 12

In general using a bitwise operator when really you want to perform a division is a bad practice. 一般情况下,当你真正想要执行除法时使用按位运算符是一种不好的做法。 It is probably a premature optimization made because the programmer thought it would be faster to perform a bitwise operation instead of a division. 这可能是一个过早的优化,因为程序员认为执行按位操作而不是除法更快。 It would be much clearer to write a division and I wouldn't be surprised if the performance of the two approaches is comparable. 写一个部门会更清楚,如果两种方法的表现相当,我也不会感到惊讶。

*The expression x >> 1 gives the same result as x / 2 for all positive integers and all negative even integers. *对于所有正整数和所有负偶数整数,表达式x >> 1给出与x / 2相同的结果。 However it gives a different result for negative odd integers. 然而,它给出了负奇数整数的不同结果。 For example -101 >> 1 == -51 whereas -101 / 2 == -50 . 例如-101 >> 1 == -51-101 / 2 == -50

**Actually the median is only defined this way if the list has an odd number of elements. **实际上,如果列表具有奇数个元素,则仅以这种方式定义中值。 For an even number of elements this method will strictly speaking not give the median. 对于偶数个元素,这种方法严格来说不会给出中位数。

It's a bitwise opperator a definition i just grabbed from http://en.wikibooks.org/wiki/C_Sharp_Programming/Operators : 这是一个按位操作符,我刚从http://en.wikibooks.org/wiki/C_Sharp_Programming/Operators中获取了一个定义:

The binary operator >> evaluates its operands and returns the resulting first argument right-shifted by the number of bits specified by the second argument. 二元运算符>>计算其操作数,并返回由第二个参数指定的位数右移的结果第一个参数。 It discards low-order bits that are shifted beyond the size of its first argument and sets new high-order bits to the sign bit of the first argument, or to zero if the first argument is unsigned. 它丢弃超出其第一个参数大小的低位,并将新的高位设置为第一个参数的符号位,或者如果第一个参数是无符号则设置为零。

Its basically dividing by 2... 它基本上除以2 ......

>> is the bitwise right-shift operator, and shifting colorList.Count to the right by 1 is more or less equivalent to colorList.Count / 2 . >>是按位右移运算符,将colorList.Count向右移1则或多或少等于colorList.Count / 2

A right shift of a >> b can be defined as a / 2 ^ b . a >> b右移可以定义为a / 2 ^ b

As for why you would use a right-shift rather than divide by 2, I have no idea. 至于为什么你会使用右移而不是除以2,我不知道。

C programmers (of which I've been one for over 20 years) routinely have used bitwise shifts for multiplying or dividing by powers of 2. The reason was that in older architectures (think 2 MHz processor, 32K of memory, and no disk) it was significantly faster to shift and generally compiled to a single machine instruction. C程序员(其中我已经超过20年)经常使用按位移位乘以或除以2的幂。原因在于旧架构(想想2 MHz处理器,32K内存,没有磁盘)它的转换速度明显更快,并且通常编译为单个机器指令。 Even though I write primarily C# now, I still, as a mater of habit, sometimes use this trick. 即使我现在主要编写C#,但作为一个习惯,我仍然有时会使用这个技巧。 Another common C convention that most C# programmers have never seen is having an assignment embedded within a conditional. 大多数C#程序员从未见过的另一个常见的C约定是在条件中嵌入了赋值。 For example: 例如:

if ( (a = getmeanumber()) == 0 )
   /* do something */ ;

Anyway, as to the original question and the reasons for its use, they largely no longer exist except with the limited realm of embedded programming where every byte and clock cycle could matter. 无论如何,关于原始问题及其使用原因,它们基本上不再存在,除了嵌入式编程的有限领域,其中每个字节和时钟周期都很重要。

It's not very readable code, basically it just divides the number by 2. 它不是非常易读的代码,基本上它只是将数字除以2。

>> is the shift-right operator, shifting all bits one position to the right. >>是右移操作符,将所有位向右移动一位。

0110 (6) becomes 0011 (3) 0110(6)成为0011(3)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM