Best practices for circular shift (rotate) operations in C++

前端 未结 16 1556
情深已故
情深已故 2020-11-22 00:09

Left and right shift operators (<< and >>) are already available in C++. However, I couldn\'t find out how I could perform circular shift or rotate operations.

16条回答
  •  再見小時候
    2020-11-22 00:58

    Overload a function:

    unsigned int rotate_right(unsigned int x)
    {
     return (x>>1 | (x&1?0x80000000:0))
    }
    
    unsigned short rotate_right(unsigned short x) { /* etc. */ }
    

提交回复
热议问题