Reverse Bits

来源:互联网 发布:c语言打开文本文件 编辑:程序博客网 时间:2024/06/04 18:52
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;    }}

0 0