Search code examples
c#bitwise-operators

C# bitwise rotate left and rotate right


What is the C# equivalent (.NET 2.0) of _rotl and _rotr from C++?


Solution

  • Is this what you are trying to do?

    Jon Skeet answered this in another site

    Basically what you want is

    (for left)

    (original << bits) | (original >> (32 - bits))
    

    or

    (for right)

    (original >> bits) | (original << (32 - bits))
    

    Also, as Mehrdad has already suggested, this only works for uint, which is the example that Jon gives as well.