190. 颠倒二进制位

116 阅读1分钟

public class Solution {
    // you need treat n as an unsigned value
    public int reverseBits(int n) {
        int res = 0;
        for (int i = 0; i < 32; i++) {//不能写成n != 0,这是翻转。
            res = res << 1;
            res = res + (n & 1);
            n = n >>> 1;
        }
        return res;
    }
}