知识点
位运算
思路
由翻转,我们可以反向,从右往左遍历,赋值到res中的时候从左往右赋值即可(使用左移运算)
代码c++
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param n int整型
* @return int整型
*/
int reverseBits(int n) {
// write code here
int res=0;
for(int i=0;i<32;i++)
{
res=(res<<1)+(n&1);//res左移再赋值最后一位
n>>=1;//n右移
}
return res;
}
};