题解 | #牛群的编码反转#
牛群的编码反转
https://www.nowcoder.com/practice/fbbef1b8d84b45a49f95ebf63a3b353b
考察数字位移。通过循环和位运算移位后就可以得到结果
完整Java代码如下所示
import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param n int整型 * @return int整型 */ public int reverseBits (int n) { int res = 0; for (int i = 0; i < 32; i ++) { res = (res << 1) + (n & 1); n >>= 1; } return res; } }