Problem with Bitwise Barrel Shift Rotate left and right in C#

前端 未结 3 1627
逝去的感伤
逝去的感伤 2021-01-23 02:31

In C++ I have code like this.

    static UInt32 rol(UInt32 value, UInt32 bits)
    {
        bits &= 31;
        return ((value << bits) | (value >&         


        
相关标签:
3条回答
  • 2021-01-23 02:53

    You should use int type for the right side variable in shift operators.

    0 讨论(0)
  • 2021-01-23 03:01

    The right operand must be always type int.

     int x << int bits
     uint x << int bits
     long x << int bits
     ulong x << int bits
    
    0 讨论(0)
  • 2021-01-23 03:08

    You will have to cast the right side of the bitshift operator to int. If you cast like (int)(32 - bits), it should not affect your intended purpose. The right side is just expecting an int, probably because it's simpler that way and highly unlikely you'll ever want to shift more than 2 billion bits.

    0 讨论(0)
提交回复
热议问题