LeetCode题解:Reverse Bits
2015-08-26 20:35
281 查看
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位二进制数的顺序
解决思路:通过与1进行与运算得到最低位数,右移去掉已运算部分,左移进位
代码:
For example, given input 43261596 (represented in binary as 00000010100101000001111010011100), return 964176192 (represented in binary as 00111001011110000010100101000000).
题意:逆转32位二进制数的顺序
解决思路:通过与1进行与运算得到最低位数,右移去掉已运算部分,左移进位
代码:
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 += (n & 1); n >>= 1; if(i < 31){ result <<= 1; } } return result; } }
相关文章推荐
- django 单独测试模块
- nginx之Web服务器集群-基础知识
- Python读取pcap文件
- 【吐槽】中国网站和国外网站的不一样
- cocos2dx 玩转简单数据存储
- 应对非均衡数据集分类问题的八大策略
- 代码大全2(3)
- poj2184Cow Exhibition(01背包)
- exit和return的区别
- LinuxI/O端口和I/O内存的访问接口
- http头需知
- 九度oj 1151
- 2015 多校联赛 ——HDU5410(dp)
- C/C++编程题之IP地址转整数
- 2015 多校联赛 ——HDU5410(dp)
- 【c++版数据结构】之用带头节点的单链表实现一元多项式(C语言版)
- Oracle中的PLsql的符号解释大全
- JAVA spring hibernate 多数据源配置记录
- POJ 3164 Command Network
- ios隐藏状态栏