LeetCode_190. 颠倒二进制位

ps:容器左移一格然后把移出来的末位放上原数n的末尾
public class S_190 {
    public int reverseBits(int n) {
        int res = 0;
        for (int i = 0; i < 32; i ++) {
            // 左移运算符 res << 1,相当于res乘以2
            res <<= 1;
            res += n & 1;
            n >>= 1;
        }
        return res;
    }
}

猜你喜欢

转载自blog.csdn.net/king1994wzl/article/details/83036621