leetCode-190:Reverse Bits 发表于 2023-08-18 分类于 leetCode 问题描述给定一个32位无符号整数,要求将其按位翻转。题目链接:点我 样例输入输出 输入:n = 00000010100101000001111010011100 输出:00111001011110000010100101000000 输入:n = 00000000000000000000000000000001 输出:10000000000000000000000000000000 问题解法按位运算倒置,代码如下 12345678910111213public class Solution { // you need treat n as an unsigned value public int reverseBits(int n) { int result = 0; for (int i = 0; i < 32; i++) { int last = n & 1; result = result | (last << (31 - i)); n = n >>> 1; } return result; }}