题解 | #牛群的编码反转#
牛群的编码反转
https://www.nowcoder.com/practice/fbbef1b8d84b45a49f95ebf63a3b353b
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param n int整型
* @return int整型
*/
int reverseBits(int n) {
int result = 0;
for(int i=0;i<32;i++){
// 左移一位
result<<=1;
// 判断 n 的最低位是否为 1
if((n&1)==1){
// 将 result 的最低位设为 1
result|=1;
}
// 将 n 右移一位
n>>=1;
}
return result;
}
};
