翻转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的更多相关文章

  1. Reverse bits - 按位反转一个int型数字

    Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in ...

  2. Codeforces F. Bits And Pieces(位运算)

    传送门. 位运算的比较基本的题. 考虑枚举\(i\),然后二进制位从大到小考虑, 对于第\(w\)位,如果\(a[i][w]=1\),那么对\(j.k\)并没有什么限制. 如果\(a[i][w]=0\ ...

  3. 190. Reverse Bits -- 按位反转

    Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in ...

  4. LeetCode OJ:Reverse Bits(旋转bit位)

    Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in ...

  5. Java位运算总结-leetcode题目

    按位操作符只能用于整数基本数据类型中的单个bit中,操作符对应表格: Operator Description & 按位与(12345&1=1,可用于判断整数的奇偶性) | 按位或 ^ ...

  6. leetcode - 位运算题目汇总(下)

    接上文leetcode - 位运算题目汇总(上),继续来切leetcode中Bit Manipulation下的题目. Bitwise AND of Numbers Range 给出一个范围,[m, ...

  7. BitMap - leetcode [位运算]

    136. Single Number 因为A XOR A = 0,且XOR运算是可交换的,于是,对于实例{2,1,4,5,2,4,1}就会有这样的结果: (2^1^4^5^2^4^1) => ( ...

  8. 【LeetCode】190. Reverse Bits

    题目: Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented ...

  9. LeetCode编程训练 - 位运算(Bit Manipulation)

    位运算基础 说到与(&).或(|).非(~).异或(^).位移等位运算,就得说到位运算的各种奇淫巧技,下面分运算符说明. 1. 与(&) 计算式 a&b,a.b各位中同为 1 ...

随机推荐

  1. 【bzoj3309】DZY Loves Math 莫比乌斯反演+线性筛

    Description 对于正整数n,定义f(n)为n所含质因子的最大幂指数.例如f(1960)=f(2^3 * 5^1 * 7^2)=3, f(10007)=1, f(1)=0. 给定正整数a,b, ...

  2. Python3之subprocess模块

    一.简介 subprocess最早在2.4版本引入.用来生成子进程,并可以通过管道连接他们的输入/输出/错误,以及获得他们的返回值. # subprocess用来替换多个旧模块和函数 os.syste ...

  3. 数据结构实验之图论七:驴友计划 ( 最短路径 Dijkstra 算法 )

    数据结构实验之图论七:驴友计划 Time Limit: 1000 ms           Memory Limit: 65536 KiB Submit Statistic Discuss Probl ...

  4. [USACO10MAR]伟大的奶牛聚集 BZOJ 1827 树形dp+dfs

    题目描述 Bessie is planning the annual Great Cow Gathering for cows all across the country and, of cours ...

  5. ansible基本模块-server

    ansible   XXX   -m   service   -a  "name=XXX   state=started   enabled=yes"

  6. Hibernate学习笔记(五)—— Hibernate查询方式

    一.对象图导航查询 对象图导航查询方式是根据已经加载的对象,导航到他的关联对象.它利用类与类之间的关系来查询对象.比如要查找一个联系人对应的客户,就可以由联系人对象自动导航找到联系人所属的客户对象.当 ...

  7. P2257 YY的GCD (莫比乌斯反演)

    [题目链接] https://www.luogu.org/problemnew/show/P2257 // luogu-judger-enable-o2 /* -------------------- ...

  8. 关于:“无法序列化会话状态。在“StateServer”或“SQLServer”模式下,...的问题

    关于:“无法序列化会话状态.在“StateServer”或“SQLServer”模式下,...的问题 错误描述: 无法序列化会话状态.在“StateServer”或“SQLServer”模式下,ASP ...

  9. 洛谷 P3239 / loj 2112 [HNOI2015] 亚瑟王 题解【期望】【DP】

    ???看不懂的期望DP 题目描述 小 K 不慎被 LL 邪教洗脑了,洗脑程度深到他甚至想要从亚瑟王邪教中脱坑. 他决定,在脱坑之前,最后再来打一盘亚瑟王.既然是最后一战,就一定要打得漂亮.众所周知,亚 ...

  10. 字典序的第K小数字

    今天zyb参加一场面试,面试官听说zyb是ACMer之后立马抛出了一道算法题给zyb:有一个序列,是1到n的一种排列,排列的顺序是字典序小的在前,那么第k个数字是什么?例如n=15,k=7, 排列顺序 ...