当前位置 博文首页 > Liu,:Leetcode——颠倒二进制位
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++){
//res持续左移,每次插入n的最后一位,n保持右移
res = res << 1;
res = res + (n & 1);
// n & 1:获取n的最后一位
n = n >> 1;
}
return res;
}
}
cs