位运算(3)——Reverse Bits
翻转32位无符号二进制整数
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).
Follow up:
If this function is called many times, how would you optimize it?
public class Solution {
// you need treat n as an unsigned value
public int reverseBits(int n) {
int res = 0;
for(int i=0; i<32; i++) {
res = res + (n & 1);
if(i < 31) {
res = res << 1;
}
n = n >> 1;
}
return res;
}
}
另一种写法:
public int reverseBits(int n) {
int res = 0;
for(int i=0; i<32; i++) {
if((n & 1) == 1) {
res = (res << 1) + 1;
} else {
res = res << 1;
}
n = n >> 1;
}
return res;
}
思路都是从右到左判断0/1,加到res。
位运算(3)——Reverse Bits的更多相关文章
- Reverse bits - 按位反转一个int型数字
Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in ...
- Codeforces F. Bits And Pieces(位运算)
传送门. 位运算的比较基本的题. 考虑枚举\(i\),然后二进制位从大到小考虑, 对于第\(w\)位,如果\(a[i][w]=1\),那么对\(j.k\)并没有什么限制. 如果\(a[i][w]=0\ ...
- 190. Reverse Bits -- 按位反转
Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in ...
- LeetCode OJ:Reverse Bits(旋转bit位)
Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in ...
- Java位运算总结-leetcode题目
按位操作符只能用于整数基本数据类型中的单个bit中,操作符对应表格: Operator Description & 按位与(12345&1=1,可用于判断整数的奇偶性) | 按位或 ^ ...
- leetcode - 位运算题目汇总(下)
接上文leetcode - 位运算题目汇总(上),继续来切leetcode中Bit Manipulation下的题目. Bitwise AND of Numbers Range 给出一个范围,[m, ...
- BitMap - leetcode [位运算]
136. Single Number 因为A XOR A = 0,且XOR运算是可交换的,于是,对于实例{2,1,4,5,2,4,1}就会有这样的结果: (2^1^4^5^2^4^1) => ( ...
- 【LeetCode】190. Reverse Bits
题目: Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented ...
- LeetCode编程训练 - 位运算(Bit Manipulation)
位运算基础 说到与(&).或(|).非(~).异或(^).位移等位运算,就得说到位运算的各种奇淫巧技,下面分运算符说明. 1. 与(&) 计算式 a&b,a.b各位中同为 1 ...
随机推荐
- Sample-Code:Bing Search API
Demo link: http://code.msdn.microsoft.com/windowsazure/How-to-use-bing-search-API-4c8b287e Aspx Code ...
- SDUT OJ 数据结构实验之图论四:迷宫探索
数据结构实验之图论四:迷宫探索 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Descrip ...
- qt线程睡眠
头文件 #include <QThread> 接口函数: void QThread::sleep ( unsigned long secs ) [static protected] v ...
- IntelliJ IDEA 把Maven项目导出可执行jar包
2017年04月05日 14:05:08 waterimelon 阅读数:1574 标签: intellij ideamaven 更多 个人分类: idea 第一步 第二步 第三步
- 老实pear_Excel 操作类 Spreadsheet_Excel_Writer 常用参数说明
(如果是PHP5项目就不用往下看了,因为PHP5项目可以直接用PHPExcel,方便快捷) 手上有个PHP4的修改项目,要修改Excel的导出,然后再把导出的Excel再导入到系统里. 在导入的时候, ...
- SGU - 507 启发式合并维护平衡树信息
题意:给定一颗树,每个叶子节点\(u\)都有权值\(val[u]\),求每个非叶子节点子树的最小叶子距离,若该子树只有一个叶子节点,输出INF 貌似本来是一道树分治(并不会)的题目,然而可以利用平衡树 ...
- Gson反序列化泛型实例
1 package com.ppmoney.g2.mapper; import com.google.common.reflect.TypeToken; import com.google.gson. ...
- hibernateUtil类
package com.test.Util; import org.hibernate.SessionFactory; import org.hibernate.cfg.AnnotationConfi ...
- ThinkPHP5.0的助手函数汇总
load_trait:快速导入Traits,PHP5.5以上无需调用 /** * 快速导入Traits PHP5.5以上无需调用 * @param string $class trait库 * @pa ...
- [转] SpringBoot RESTful 应用中的异常处理小结
[From] https://segmentfault.com/a/1190000006749441 SpringBoot RESTful 应用中的异常处理小结 永顺 2016年08月29日发布 赞 ...