Bitwise rotation???

/* This should work. I tried it in VB real quick, but not C#
Should be a lot quicker than the other version as well
Warning: may have problem with signed integers
I don't know how .NET treats them on bit shifts */

val = (val << 1) | (val >> 31);
 
I've found this...


#define ROTR(x,y,b) (((x)>>(y&((b)-1))) | ((x)<<((b)-(y&((b)-1)))))

it's for C++

could someone translate it to C# ?

x is value, y how many rotates and b how many bits the value has... Thx

isn't there a simplier way? I'm really confused!
 
I'm very new to C#, but what if you created a generic function like this:

Code:
    uint RolRight(uint x, byte y, byte b) // number, shift, bits
    {
      //#define ROTR(x,y,b) (((x)>>(y&((b)-1))) | ((x)<<((b)-(y&((b)-1)))))
      return (x >> (y & (b - 1))) | (x << (b - (y & (b - 1))));
    }

And called it like one of these:

byte Ret = (byte)RolRight(0x81, 1, 8);
ushort Ret = (ushort)RolRight(0x8001, 1, 16);
uint Ret = RolRight(0x80000001, 1, 32);
 
Back
Top