Java - Circular shift using bitwise operations

后端 未结 5 1795
我寻月下人不归
我寻月下人不归 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:28

    You mean you want the bits rotated off the right-hand side to appear on the left?

    return Integer.rotateRight(bits, k);
    

    Example:

    int n = 0x55005500; // Binary 01010101000000000101010100000000
    int k = 13;
    System.err.printf("%08x%n", Integer.rotateRight(n, k));
    

    output:

    a802a802 // Binary 10101000000000101010100000000010
    

提交回复
热议问题