public class Solution {
// you need treat n as an unsigned value
public int reverseBits(int n) {
int ans = 0;
for(int i=1;i<=32;i++) {
ans = ans << 1;
ans += (n & 1);
n = n >>> 1;
}
return ans;
}
}
利用左移右移,与,或等位运算。每次取n末尾的bit作为ans的bit,然后n右移,ans左移。