Java - Circular shift using bitwise operations

后端 未结 5 1799
我寻月下人不归
我寻月下人不归 2020-12-05 00:58

I am wondering how to implement a circular right shift by k of the bitstring represented by the int bits.

public int r         


        
5条回答
  •  时光说笑
    2020-12-05 01:12

    This should work:

     return (bits >>> k) | (bits << (Integer.SIZE - k));
    

    Also see the Wikipedia article on circular shifts.

提交回复
热议问题