Reverse Bits
程序员文章站
2022-05-02 19:09:21
...
Reverse bits of a given 32 bits unsigned integer.
For example, given input 43261596 (represented in binary as 00000010100101000001111010011100), return 964176192 (represented in binary as 00111001011110000010100101000000).
给定一个无符号的整数,将它的每个比特位都逆转,返回逆转之后新的数。通过位运算,取32个不同位上的数字,同时通过左移来得到结果。代码如下:
For example, given input 43261596 (represented in binary as 00000010100101000001111010011100), return 964176192 (represented in binary as 00111001011110000010100101000000).
给定一个无符号的整数,将它的每个比特位都逆转,返回逆转之后新的数。通过位运算,取32个不同位上的数字,同时通过左移来得到结果。代码如下:
public 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++) { result <<= 1; result |= (n >> i & 1); } return result; } }
上一篇: Number of 1 Bits
下一篇: Single Number III
推荐阅读
-
Reverse a String-freecodecamp算法题目
-
Python 列表排序方法reverse、sort、sorted详解
-
Vue push() pop() shift() unshift() splice() sort() reverse() ...
-
【转载】C#中List集合使用Reverse方法对集合中的元素进行倒序反转
-
evaluate-reverse-polish-notation
-
字符串反转reverse
-
[c语言] 编写一个函数reverse_string(char * string)(递归实现)
-
JavaScript数组排序reverse()和sort()方法详解
-
在JavaScript中处理数组之reverse()方法的使用
-
万能pb_ds头文件—bits/extc++.h