是否有任何 C# 按位移位运算符可以将溢出位移动到变量的另一端?
我们称之为“<<<”
int32variable <<< numberOfBits
equals
(int32variable << numberOfBits) | (int32variable >> (32 - numberOfBits))
(假设<<和>>丢弃溢出位)
有这样的运算符吗?
Let's call it "<<<"
int32variable <<< numberOfBits
equals
(int32variable << numberOfBits) | (int32variable >> (32 - numberOfBits))
(Assuming << and >> discards overflown bits)
There is such an operator?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
C# 中没有原始运算符可以完成您想要的操作。
忽略方法调用的性能影响(如果您正在执行位移操作,您可能会关心),这将是 Int32 上扩展方法的良好候选者。
There is no primitive operator in C# to do what you're looking for.
Ignoring the performance implications of a method call (if you're doing bitshift operations, you might care), this would be a good candidate for an extension method on Int32.
这就是所谓的位旋转,而 C# 没有这样的运算符。
这是一篇有趣的帖子:
有没有办法执行循环C# 中的位移位?
请注意,您的 int32integer 应该是
uint
类型(无符号 int)。我相信位旋转是 Intel 指令集中的指令,但据我所知 CLR 没有位旋转指令。
That would be a called a bit rotate and C# does not have such an operator.
Here's an interesting post:
Is there a way to perform a circular bit shift in C#?
Note that your int32integer should be of type
uint
(unsigned int).I believe bit rotate is an instructions in the Intel instruction set but as far as I know the CLR does not have bit rotate instructions.
这称为按位循环。 其他语言有,但C#没有。 有关更多详细信息,请参阅此问题,但其要点是你目前的方法已经是你能做的最好的了。
This is called a bitwise rotation. Other languages have it, but C# does not. See this question for a little more detail, but the gist of it is that your current approach is about as good as you can do.
我认为 C# 没有。 请参阅此页面以获取 C# 运算符列表:http:// /msdn.microsoft.com/en-us/library/6a71f45d(vs.71).aspx。
我认为您在问题中所展示的方式可能是最快的方法。 我认为 Java 可能有一个像您所追求的那样的移位运算符,但作为一名 Java 程序员,我无法证实这一点。
I don't think that there is for C#. See this page for the list of C# operators: http://msdn.microsoft.com/en-us/library/6a71f45d(vs.71).aspx.
I think that they way you have shown in your question is probably the quickest way to do it. I think that Java may have a shift operator like the one you are after, but not being a Java programer I can't confirm that.