R
Ramtin Kazemi
Hi
How can i perform bitwise rotation in C#?
How can i perform bitwise rotation in C#?
Hi
How can i perform bitwise rotation in C#?
DeveloperX said:int x = 1;
x<<=3;
Console.WriteLine(x.ToString()); //writes 8
DeveloperX said:int x = 1;
x<<=3;
Console.WriteLine(x.ToString()); //writes 8
Hi
How can i perform bitwise rotation in C#?
Hi
How can i perform bitwise rotation in C#?
Hi Ramtin,
You'll probably need to add some error checking & input validation,
but something like this seems to work:
int rotateBits = 3;
int dataSize = 16;
bool rotateleft = true; // false = right
uint value = 0xABCD;
uint mask = (uint)((1 << dataSize) - 1);
if (rotateleft)
value = ((value << rotateBits) | (value >> (dataSize - rotateBits)))
& mask;
else
value = ((value >> rotateBits) | (value << (dataSize - rotateBits)))
& mask;
John