Reverse Bits

<<< or >>>是无符号移位

public class Solution {
    // you need treat n as an unsigned value
    // http://blog.csdn.net/woliuyunyicai/article/details/44198311
    public int reverseBits(int n) {
        int res = 0, tmp=0;
        while(n!=0){
            res = (res<<=1) |(n&1);
            n>>>=1;
            tmp++;
        }
        if(tmp<32){
            res<<=(32-tmp);
        }
        return res;
    }
}
res<<=(32-tmp);

按位翻转,一定要到前边去啊, 完全颠倒

0001101变成
1011000
posted @ 2015-05-22 03:41  世界到处都是小星星  阅读(94)  评论(0编辑  收藏  举报