Jas*_*n Z 34 c# bit-manipulation
我知道以下是真的
int i = 17; //binary 10001
int j = i << 1; //decimal 34, binary 100010
Run Code Online (Sandbox Code Playgroud)
但是,如果你换得太远,那些位就会掉线.发生这种情况的原因与您正在使用的整数大小有关.
有没有办法执行移位,以便位旋转到另一侧?我正在寻找一个单独的操作,而不是for循环.
Chr*_*org 48
如果您知道类型的大小,您可以执行以下操作:
uint i = 17;
uint j = i << 1 | i >> 31;
Run Code Online (Sandbox Code Playgroud)
...将执行32位值的循环移位.
作为循环移位左移n位的推广,在ab位变量上:
/*some unsigned numeric type*/ input = 17;
var result = input << n | input >> (b - n);
Run Code Online (Sandbox Code Playgroud)
jai*_*unt 11
一年前,我将为我的本科毕业论文实施MD4.这是我使用UInt32实现循环位移.
private UInt32 RotateLeft(UInt32 x, Byte n)
{
return UInt32((x << n) | (x >> (32 - n)));
}
Run Code Online (Sandbox Code Playgroud)
由于 .NET Core 3.0 及更高版本BitOperations.RotateLeft(),BitOperations.RotateRight()因此您可以使用类似的东西
BitOperations.RotateRight(12, 3);
BitOperations.RotateLeft(34L, 5);
Run Code Online (Sandbox Code Playgroud)
在以前的版本中,您可以在 Microsoft.VisualStudio.Utilities 中使用BitRotator.RotateLeft()和BitRotator.RotateRight()