位运算(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 ...
随机推荐
- DHCP与PPPOE 区别
1.静态IP的方式,如果是占用一个INTERNET的IP的话,上网都是很贵的,当然这个也是最方便的,开机就能上网,不用做任何拨号或者认证的过程.2.PPPOE,只是多了一个获得IP的过程,一旦获得了I ...
- 【BZOJ1053】[HAOI2007]反素数 (搜索+数论)
\([POI2002][HAOI2007]\)反素数 题目描述 对于任何正整数x,其约数的个数记作\(g(x)\).例如\(g(1)=1.g(6)=4\). 如果某个正整数x满足:\(g(x)> ...
- css3中的display:-webkit-box的用法
一. css weui-media-box__desc { color: #999999; font-size: 13px; line-height: 1.2; overflow: hidden; t ...
- MVC5 + EF6 入门完整教程1
https://www.cnblogs.com/miro/p/4030622.html 第0课 从0开始 ASP.NET MVC开发模式和传统的WebForm开发模式相比,增加了很多"约定& ...
- CentOS 7 基础网络管理
网络服务管理 network服务是对整个计算机网络服务的控制,也可以理解为控制所有网卡. [root@localhost ~]# systemctl start network //开启网络服务 [r ...
- ssm框架的搭建流程
1.新建一个Maven project (1)选中create a simple project,自动配置必要的文件 (2)Packaging选择war类型.jar和war的区别就是一个是普通的jav ...
- D. Minimum Diameter Tree Round #528 (Div. 2)【树】
一.题面 题目链接 二.分析 该题注意读题的时候有强调边的权值为非负(即可以为0),此题就是求树两个叶子节点之间的最短距离.为了使两个叶子节点之间的距离最短,那么其实就是让每个最后到叶子的那条路径尽量 ...
- 108th LeetCode Weekly Contest Unique Email Addresses
Every email consists of a local name and a domain name, separated by the @ sign. For example, in ali ...
- 设置input placeholder的样式
::-webkit-input-placeholder {/*Chrome/Safari*/ font-family: 'Avenir', Helvetica, Arial, sans-serif; ...
- node.js知识点提取
javascript是脚本语言,脚本语言都需要一个解析器才能运行.