Reverse Bits

public class Solution {
    // you need treat n as an unsigned value
    public int reverseBits(int n) {
        for(int i = 0; i < 16; i++) {
            n = reversePair(n, i, 32 - i - 1);
        }
        return n;
    }
    private int reversePair(int n, int right, int left) {
        int a = (n >> right) & 1;
        int b = (n >> left) & 1;
        if ((a ^ b) != 0) {
            return n = n ^ (1 << right | 1 << left);
        }
        return n;
    }
}

阅读更多
文章标签: Bit Manipulation
个人分类: Leetcode 刷题
上一篇Number of 1 Bits
下一篇Intersection of Two Linked Lists
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭