有没有办法在 C# 中执行循环位移?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35167/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there a way to perform a circular bit shift in C#?
提问by Jason Z
I know that the following is true
我知道以下是正确的
int i = 17; //binary 10001
int j = i << 1; //decimal 34, binary 100010
But, if you shift too far, the bits fall off the end. Where this happens is a matter of the size of integer you are working with.
但是,如果您移动得太远,则位会从末端脱落。发生这种情况的地方与您正在使用的整数大小有关。
Is there a way to perform a shift so that the bits rotate around to the other side? I'm looking for a single operation, not a for loop.
有没有办法执行移位以便位旋转到另一侧?我正在寻找单个操作,而不是 for 循环。
采纳答案by Chris Marasti-Georg
If you know the size of type, you could do something like:
如果您知道类型的大小,您可以执行以下操作:
uint i = 17;
uint j = i << 1 | i >> 31;
... which would perform a circular shift of a 32 bit value.
...这将执行 32 位值的循环移位。
As a generalization to circular shift left n bits, on a b bit variable:
作为对 ab 位变量的循环左移 n 位的概括:
/*some unsigned numeric type*/ input = 17;
var result = input << n | input >> (b - n);
@评论,看来 C# 确实以不同的方式对待有符号值的高位。我找到了一些关于这个的信息here在这里。我还更改了示例以使用 uint。
回答by Chris Blackwell
I have to admit, I just did a search for "C# bit rotate" and found a link to a page with a Java class that would be easily adapted to C#
我不得不承认,我只是搜索了“C# bit rotate”并找到了一个指向带有 Java 类的页面的链接,该页面可以轻松适应 C#
I also found this in Google Book which is a C++ function with similar behavior
回答by jaircazarin-old-account
One year ago I've to implement MD4 for my undergraduate thesis. Here it is my implementation of circular bit shift using a UInt32.
一年前,我必须为我的本科论文实施 MD4。这是我使用 UInt32 实现的循环位移。
private UInt32 RotateLeft(UInt32 x, Byte n)
{
return UInt32((x << n) | (x >> (32 - n)));
}
回答by yeyeyerman
Just as reference on how to do it, these two functions work perfectly for rotating the bits of 1/2word:
作为如何做的参考,这两个函数非常适合旋转 1/2word 的位:
static public uint ShiftRight(uint z_value, int z_shift)
{
return ((z_value >> z_shift) | (z_value << (16 - z_shift))) & 0x0000FFFF;
}
static public uint ShiftLeft(uint z_value, int z_shift)
{
return ((z_value << z_shift) | (z_value >> (16 - z_shift))) & 0x0000FFFF;
}
It would be easy to extend it for any given size.
对于任何给定的大小,扩展它都很容易。
回答by Dale Gerdemann
The most famous application is the solution to the Josephus problem (as discussed in Concrete Mathematics, see http://oeis.org/A006257). This is basically a puzzle with no obvious applications. In this video, I demonstrated connections between the second order Josephus problem and complete balanced trees. It's still not an application, but moving slightly in the right direction.
最著名的应用是解决约瑟夫斯问题(如具体数学中所述,参见http://oeis.org/A006257)。这基本上是一个没有明显应用的谜题。在本视频中,我演示了二阶约瑟夫斯问题和完全平衡树之间的联系。它仍然不是一个应用程序,而是朝着正确的方向略微移动。
回答by phuclv
Sincce .NET Core 3.0 and up there's BitOperations.RotateLeft()
and BitOperations.RotateRight()
so you can just use something like
由于 .NET Core 3.0 及更高版本BitOperations.RotateLeft()
,BitOperations.RotateRight()
因此您可以使用类似的东西
BitOperations.RotateRight(12, 3);
BitOperations.RotateLeft(34L, 5);
In previous versions you can use BitRotator.RotateLeft()
and BitRotator.RotateRight()
in Microsoft.VisualStudio.Utilities
在以前的版本中,您可以在 Microsoft.VisualStudio.Utilities 中使用BitRotator.RotateLeft()
和BitRotator.RotateRight()