0

Let's call it "<<<"

int32variable <<< numberOfBits

equals

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

(Assuming << and >> discards overflown bits)

There is such an operator?

Jader Dias
  • 88,211
  • 155
  • 421
  • 625
  • Does this answer your question? [Is there a way to perform a circular bit shift in C#?](https://stackoverflow.com/questions/35167/is-there-a-way-to-perform-a-circular-bit-shift-in-c) – phuclv Feb 11 '20 at 02:10

4 Answers4

4

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.

Community
  • 1
  • 1
Kevin Newman
  • 2,437
  • 1
  • 15
  • 12
  • 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 Jun 08 '09 at 02:59
1

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.

Community
  • 1
  • 1
Matt J
  • 43,589
  • 7
  • 49
  • 57
0

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
  • 59,888
  • 27
  • 145
  • 179
0

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
  • 2,637
  • 2
  • 21
  • 20