1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
|
public class ReverseBits { public int reverseBits(int n) { int j; for (int i = 0; i < 16; i++) { j = 31 - i; n = swap(n, i, j); } return n; } private int swap(int n, int left, int right) { boolean leftBit = (n & 1 << left) != 0; boolean rightBit = (n & 1 << right) != 0; n = leftBit ? setOne(n, right) : setZero(n, right); n = rightBit ? setOne(n, left) : setZero(n, left); return n; } private int setZero(int n, int position) { return n & (~(1 << position)); } private int setOne(int n, int position) { return n | (1 << position); } @Test public void test() { System.out.println(reverseBits(2)); System.out.println(setZero(3, 0)); } }
|