两个左尖括号“<<”有什么作用?在 C# 中是什么意思?
基本上都是标题里的问题。我正在查看 MVC 2 源代码:
[Flags]
public enum HttpVerbs {
Get = 1 << 0,
Post = 1 << 1,
Put = 1 << 2,
Delete = 1 << 3,
Head = 1 << 4
}
我只是好奇双左尖括号 <<
的作用。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(15)
当您写入时,
您将位组合
000000001
左移n
次,从而将n
放入 2 的指数中:所以
确实
对于一个列表来说 如此比如说 5 个项目,您的
for
将循环 32 次。When you write
You shift the bit combination
000000001
forn
times left and thus putn
into the exponent of 2:So
Really is
For a list of say 5 items your
for
will cycle 32 times.它称为左移运算符。查看文档
演示
左移
运算符的简单示例:向左移动一位相当于乘以二。事实上,移动位比标准乘法更快。让我们看一个演示这一点的示例事实:
假设我们有两种方法:
我们想像这样测试它们:
结果如下:
It is called
left-shift
operator. Take a look at the documentationSimple example that demonstrates the
left-shift
operator:Moving one bit to left is equivelant to multiple by two.In fact,moving bits are faster than standart multiplication.Let's take a look at an example that demonstrates this fact:
Let's say we have two methods:
And we want to test them like this:
Here is the results:
这将是按位左移运算符。
对于每次左移,该值实际上都会乘以 2。因此,例如,编写
value << 3
会将值乘以 8。它在内部的真正作用是将值的所有实际位移至一位。因此,如果您的值是 12(十进制),则二进制表示为
00001100
;将其向左移动一位会将其变为00011000
或 24。That would be the bitwise left shift operator.
For each shift left, the value is effectively multiplied by 2. So, for example, writing
value << 3
will multiply the value by 8.What it really does internally is move all of the actual bits of the value left one place. So if you have the value 12 (decimal), in binary that is
00001100
; shifting it left one place will turn that into00011000
, or 24.它是按位左移它的工作原理是将数字的二进制等价数字移位给定的(右侧)数字。
所以:
14 的二进制等价物是
00001110
将其移位 2 次意味着将零从右侧推入并将每个数字移至左侧,使其00111000
等于 56。在您的示例中:
依此类推。一般来说,它等于
2 ^ list.Count
(2 的 list.Count 次方)It is Bitwise shift left it works by shifting digits of binary equivalent of number by the given (right hand side) numbers.
so:
binary equivalent of 14 is
00001110
shifting it 2 times means pushing zero from right hand side and shifting each digit to left side which make it00111000
equals to 56.In your example:
and so on. In general it is equal
2 ^ list.Count
(2 raised to the power of list.Count)这是左移运算符。它将左操作数的位模式向左移动右操作数中指定的二进制位数。
这在语义上等同于
lOperand * Math.Pow(2, rOperand)
That's the left bitshift operator. It shifts the bit pattern of the left operand to the left by the number of binary digits specified in the right operand.
This is semantically equivalent to
lOperand * Math.Pow(2, rOperand)
循环的目的很可能是生成或操作列表中项目集的所有子集。并且循环体很可能还具有良好的按位运算位(har har),即另一个左移和按位与。 (所以重写它以使用 Pow 是非常愚蠢的,我简直不敢相信有这么多人实际上建议这样做。)
The purpose of the loop is most likely to generate or operate on all subsets of the set of items in the list. And the loop body most likely also has a good bit (har har) of bitwise operations, namely both another left-shift and bitwise-and. (So rewriting it to use Pow would be mighty stupid, I can hardly believe there were so many people that actually suggested that.)
那是位移动。它基本上只是通过在右侧添加 0 将位移动到左侧。
更多信息请访问 http://www.blackwasp.co.uk/CSharpShiftOperators.aspx
Thats bit shifting. Its basically just moving the bits to the left by adding 0's to the right side.
More info at http://www.blackwasp.co.uk/CSharpShiftOperators.aspx
除了 Selman22 的答案之外,还有一些示例:
我将列出
list.Count
的一些值以及循环是什么:等等。
In addition to Selman22's answer, some examples:
I'll list some values for
list.Count
and what the loop would be:And so forth.
“左移一位。” <代码>1 << 0 表示“取整数值 1 并将其位左移零位”。即,
00000001
保持不变。 <代码>1 << 1 表示“取整数值 1 并将其位左移一位”。00000001
变为00000010
。"Bit shift left."
1 << 0
means "take the integer value 1 and shift its bits left by zero bits." I.e.,00000001
stays unchanged.1 << 1
means "take the integer value 1 and shift its bits left one place."00000001
becomes00000010
.它的 (<<) 是按位左移运算符,它移动二进制对象的位值。左操作数指定要移位的值,右操作数指定值中的位要移位的位置数。
在你的情况下,如果 list.count 的值为 4 那么循环将运行直到 i < (1<<4)
即 16 (00010000)
00000001 << 4 = 00010000(16)
Its (<<) a bitwise left shift operator, it moves the bit values of a binary object. The left operand specifies the value to be shifted and the right operand specifies the number of positions that the bits in the value are to be shifted.
In your case if the value of list.count is 4 then loop will run till i < (1<< 4)
which is 16 (00010000)
00000001 << 4 = 00010000(16)
它隐含在许多答案中,但从未直接说明过......
对于将二进制数向左移动的每个位置,都会将该数字的原始值加倍。
例如,
十进制 5 二进制左移一位是十进制 10,或十进制 5 加倍。
十进制 5 二进制左移 3 就是十进制 40,或者十进制 5 加倍 3 倍。
It is implied in a number of answers but never stated directly...
For every position that you shift a binary number left, you double the original value of the number.
For example,
Decimal 5 binary shifted left by one is decimal 10, or decimal 5 doubled.
Decimal 5 binary shifted left by 3 is decimal 40, or decimal 5 doubled 3 times.
表达式
(1 << N)
使用 位C# 中的 Shift。在本例中,它用于执行 2^N 的快速整数计算,其中 n 为 0 到 30。
对于不了解位移位工作原理的
年轻的自创者开发人员来说,Windows Calc 是一个很好的工具在程序员模式下,可以直观地看到移位对不同大小的有符号数的影响。Lsh
和Rsh
函数分别相当于<<
和>>
。在循环条件内使用 Math.Pow 进行评估(在我的系统上)大约比 N = 10 的问题代码慢 7 倍,这是否重要取决于上下文。
将“循环计数”缓存在单独的变量中会稍微加快速度,因为涉及列表长度的表达式不需要在每次迭代时重新计算。
The expression
(1 << N)
uses a Bit Shift in c#.In this case it's being used to perform a fast integer evalution of 2^N, where n is 0 to 30.
A good tool for
young whippersnappersdevelopers that don't understand how bit shifts work is Windows Calc in programmer mode, which visualises the effect of shifts on signed numbers of various sizes.The
Lsh
andRsh
functions equate to<<
and>>
respectively.Evaluating using Math.Pow inside the loop condition is (on my system) about 7 times slower than the question code for N = 10, whether this matters depends on the context.
Caching the "loop count" in a separate variable would speed it up slightly as the expression involving the list length would not need to be re-evaluated on every iteration.
之前的答案已经解释了它的作用,但似乎没有人猜测为什么。在我看来,这段代码的原因很可能是循环迭代列表中成员的每个可能的组合——这是我能理解为什么你想要迭代最多 2^{list.数数}。因此,变量 i 的命名会很糟糕:它的位代表列表中项目的组合,而不是索引(这就是我通常将“i”解释为的含义),因此(例如)如果
i
的位零被设置((i & (1 << 0)) != 0
),则可以选择第一项,如果位 1 被设置 ((i & (1 << 1)) != 0
) 等等。 <代码>1 <<因此,list.Count 是与列表中的有效项目组合不对应的第一个整数,因为它将指示选择不存在的list[list.Count]
。Previous answers have explained what it does, but nobody seems to have taken a guess as to why. It seems quite likely to me that the reason for this code is that the loop is iterating over each possible combination of members of a list -- this is the only reason I can see why you would want to iterate up to 2^{list.Count}. The variable
i
would therefore be badly named: instead of an index (which is what I usually interpret 'i' as meaning), its bits represent a combination of items from the list, so (for example) the first item may be selected if bit zero ofi
is set ((i & (1 << 0)) != 0
), the second item if bit one is set ((i & (1 << 1)) != 0
) and so on.1 << list.Count
is therefore the first integer that does not correspond to a valid combination of items from the list, as it would indicate selection of the non-existantlist[list.Count]
.我知道这个答案已经基本解决了,但我认为可视化可能会对某人有所帮助。
I know this answer is pretty much solved, but I thought the visualization might help someone.
<<
是左移运算符。如果你有数字 3,即二进制的 00000011,你可以写3 << 2
,即 00001100,即十进制的 12。<<
is the left bit shift operator. If you have the number 3, which is 00000011 in binary, you can write3 << 2
, which is 00001100, or 12 in decimal.