Monday, February 05, 2007

C# .NET Barrel Shifters

In case anyone needs the code for it ;P
private byte LeftBarrelShift(byte b, uint amount)
{
amount %= 8;
byte temp;
for (uint i = 0; i < amount; i++)
{
temp = (byte)(b >> 7);
b <<= 1;
b += temp;
}
return b;
}

private byte RightBarrelShift(byte b, uint amount)
{
amount %= 8;
byte temp;
for (uint i = 0; i < amount; i++)
{
temp = (byte)(b % 2);
b >>= 1;
temp <<= 7;
b += temp;
}
return b;
}

No comments: