1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| public class Solution { public int reverseBits(int n) { for (int i = 0; i < 16; i++) { int lowBit = (n >>> i) & 1; int highBit = (n >>> (31 - i)) & 1; if (lowBit != highBit) { if (lowBit == 0) { n &= ~(1 << (31 - i)); n |= (1 << i); } else { n |= (1 << (31 - i)); n &= ~(1 << i); } } }
return n; } }
|