Leetcode_190_Reverse Bits
来源:互联网 发布:阿里云企业邮箱客户端 编辑:程序博客网 时间:2024/06/02 01:54
本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/46868017
Reverse bits of a given 32 bits unsigned integer.
For example, given input 43261596 (represented in binary as 00000010100101000001111010011100), return 964176192 (represented in binary as00111001011110000010100101000000).
思路:
(1)题意为给定无符号32位整数,求该整数翻转(对应的二进制数的翻转)后所得的整数值。
(2)该题主要考察位运算。由于限制位数为32位,所以只需对待处理的整数n进行32次右移位,每当低位&1的结果为1,说明低位为1,此时将待输出的目标整数(默认值为0)左移动一位并加上1;每当低位&1的结果为0,说明低位为0,此时将待输出的目标整数左移一位即可;循环直到移动完32次,所得目标整数即为所求。
(3)详情见下方代码。希望本文对你有所帮助。
算法代码实现如下:
/** * * @author lqq * */public class Reverse_Bits {public int reverseBits(int n) {int value = 0;// 32位无符号数for (int i = 0; i < 32; ++i) {if ((n & 1) == 1) {value = (value << 1) + 1; // 左移动n >>= 1;} else {value = value << 1;n >>= 1; // 右移}}return value;}}
0 0
- Leetcode_190_Reverse Bits
- Bits
- Bits*
- UVA_446_Kibbles "n" Bits "n" Bits "n" Bits
- Persistent Bits
- Changing Bits
- leetcode_num190_Reverse Bits
- Reverse Bits
- Reverse Bits
- Reverse Bits
- Reverse Bits
- Reverse Bits
- Reverse Bits
- Reverse Bits
- Reverse Bits
- Reverse Bits
- Reverse Bits
- Reverse Bits
- 知识点——网络
- 监控JVM内存?
- LintCode - 更新二进制位
- Codeforces Round #311 (Div. 2) A B C D
- GNU-LIBC源码学习之strlen
- Leetcode_190_Reverse Bits
- IOS开发-06.提示框
- leetcode 69:Sqrt(x)
- 对类的继承的理解
- Android使用Volley保持与服务器的会话
- java jar 打包命令
- 数组配对(算法)
- PAT 数据结构 08-排序4. The World's Richest (25)
- 大话设计模式(一 面试受挫——代码无错就是好?)