tags:

views:

1199

answers:

4

Let's call it "<<<"

int32variable <<< numberOfBits

equals

(int32variable << numberOfBits) | (int32variable >> (32 - numberOfBits))

(Assuming << and >> discards overflown bits)

There is such an operator?

A: 

There is no primitive operator in C# to do what you're looking for.

Ignoring the performance implications of a method call (if you're doing bitshift operations, you might care), this would be a good candidate for an extension method on Int32.

Michael Petrotta
+3  A: 

That would be a called a bit rotate and C# does not have such an operator.

Here's an interesting post: Is there a way to perform a circular bit shift in C#?

Note that your int32integer should be of type uint (unsigned int).

I believe bit rotate is an instructions in the Intel instruction set but as far as I know the CLR does not have bit rotate instructions.

Bing
Indeed, according to http://en.csharp-online.net/CIL_Instruction_Set while there are three bitshift instructions in the CIL, there are no rotate instructions. Which makes sense, because no (non-assembly) programming language that I'm aware of has bit rotate operators, so why should CIL support that?
David
+1  A: 

This is called a bitwise rotation. Other languages have it, but C# does not. See this question for a little more detail, but the gist of it is that your current approach is about as good as you can do.

Matt J
A: 

I don't think that there is for C#. See this page for the list of C# operators: http://msdn.microsoft.com/en-us/library/6a71f45d(vs.71).aspx.

I think that they way you have shown in your question is probably the quickest way to do it. I think that Java may have a shift operator like the one you are after, but not being a Java programer I can't confirm that.

Caleb Vear
I see someone else answered beat me to it.
Caleb Vear