多次循环左移及运算

Sla*_*shJ 1 c# bit-shift

我不明白循环移位。

我搜索并找到了以下方法,但输入的值没有给出预期的结果:

public static uint RotateLeft(this uint value, int count)
{
    return (value << count) | (value >> (32 - count));
}
Run Code Online (Sandbox Code Playgroud)

输入应在移位时间时211给出。1583

Tho*_*ham 5

问题是您的预期结果是基于字节的,而您的代码是基于 32 位的。对于字节,请尝试以下操作:

public static byte RotateLeft(byte value, int count)
{
    return (byte)((value << count) | (value >> (8 - count)));
}
Run Code Online (Sandbox Code Playgroud)