Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hi,

I'm in need of circular shifting, which isn't supported in VB.NET.

To make things a bit more complicated (no pun intended :p ),

I only want to use the first 3 bits to do the rotation. (Just imagine a 3 bit integer)

 

I've come up with some solutions,

but I hope there is a more elegant, maybe faster solution.

 

Shift to the right:

X = ((X >> 1) Or ((X Mod 2) << 2))

 

Shift to the left:

X = (X << 1 Or ( (8 And (X << 1) ) >> 3)) And 7

 

Note:

I only need to shift by one, never more.

  • Leaders
Posted
I think that that is as simple as it is going to get. If you are going to do a bit rotation with a non-supported bit-size, you will always have to perform the carry yourself (i.e. the extra shift and 'or'). If you can safely operate under the assumption that only the least significant 3 bits are ever set, you can do away with some bit masking there.
[sIGPIC]e[/sIGPIC]
Posted

Small improvement

 

For the shl, you can do away with one of those shifts, and for the shr, you can use And instead of Mod.

 

'Assuming only the 3 least significant bits are set
'Shift right
X = (X >> 1) Or ((X And 1) << 2)
'Shift left
X = ((X And 3) << 1) Or ((X And 4) >> 2)

 

I doubt it gets much simpler than that.

 

Good luck :cool:

Never trouble another for what you can do for yourself.

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...