多次循环左移和运算

Multiple circular left shifts and operations

我不懂循环移位。

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

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

输入 211 移动 3 次后应该得到 158

问题是您的预期结果是基于字节的,而您的代码是基于 32 位的。对于字节,试试这个:

public static byte RotateLeft(byte value, int count)
{
    return (byte)((value << count) | (value >> (8 - count)));
}