Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

/* 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);

Posted

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!

Posted

I'm very new to C#, but what if you created a generic function like this:

 

   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);

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...