BiruLyu
5/25/2017 - 9:43 PM

190. Reverse Bits.java

/*
The unsigned right shift operator ">>>" shifts a zero into the leftmost position, while the leftmost position after ">>" depends on sign extension.
*/
public class Solution {
    // you need treat n as an unsigned value
    public int reverseBits(int n) {
        if (n == 0) return 0;
        int res = 0;
        for(int i = 0; i < 32; i++){
            res <<= 1;
            res |= n & 1;
            n >>>= 1;
        }
        return res;
    }
}