1 题目

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).

Follow up:
If this function is called many times, how would you optimize it?

Related problem: Reverse Integer

Credits:
Special thanks to @ts for adding this problem and creating all test cases.

Hide Tags

Bit Manipulation

 
2 分析
通过题191的思路,我们可以获得每位的数据,这样反转也就很方便了,刚开始以为要存起来,反过来赋值一遍,结果不用,两个移位的方向不一样就行了。
ps java里面向高位移位时,直接丢弃,不保存。
 
3 代码
    public int reverseBits(int n) {
int reverseN = 0;
int b;
for (int i = 0; i < 32; i++) {
b = n&1;
reverseN = (reverseN << 1) + b;
n = n >> 1;
}

另外,有个问题,不定义b的话,直接用

 reverseN = (reverseN << 1) + n&1;得不到结果,结果全部是0,原因未知。。

[leet code 190]reverse bits的更多相关文章

  1. LeetCode 190. Reverse Bits (算32次即可)

    题目: 190. Reverse Bits Reverse bits of a given 32 bits unsigned integer. For example, given input 432 ...

  2. [LeetCode] 190. Reverse Bits 颠倒二进制位

    Reverse bits of a given 32 bits unsigned integer. Example 1: Input: 00000010100101000001111010011100 ...

  3. [LeetCode] 190. Reverse Bits 翻转二进制位

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

  4. LeetCode 【190. Reverse Bits】

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

  5. Java for LeetCode 190 Reverse Bits

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

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

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

  7. Leetcode 190. Reverse Bits(反转比特数)

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

  8. 190. Reverse Bits

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

  9. 【LeetCode】190. Reverse Bits

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

随机推荐

  1. VMware Workstation 15 pro keys

    永久激活密钥UG5J2-0ME12-M89WY-NPWXX-WQH88 GA590-86Y05-4806Y-X4PEE-ZV8E0 YA18K-0WY8P-H85DY-L4NZG-X7RAD UA5D ...

  2. swift NSdata 转换 nsstring

    result = NSString(data: data, encoding: NSUTF8StringEncoding) 做HTTP 请求时 遇到 打印结果看  所以~~~

  3. Mysql遇到的坑

    2018-04-09 这个虽然跟粗心有关,但是Mysql没报错是哪般? select sum(play_count) from tb_user_login where user_id = 61 and ...

  4. 把dataset对象转换成list集合方法

    public static List<T> GetList<T>(DataTable table) where T:new() { List<T> list = n ...

  5. js jquery 取得周月年时间

    function formatDate(date) { var myyear = date.getFullYear(); var mymonth = date.getMonth() + 1; var ...

  6. 服务器重复发送SYN ACK 和 TCP_DEFER_ACCEPT设置

    现象: 以下为其他网站提供,和我遇到的情况一样. 就是服务器老是重复发送 SYN, ACK. 4414.229553  client -> server TCP 62464 > http ...

  7. 论Java的重要性

    最近,最新的世界编程语言排名最近出炉了,Java位居世界第一.          不仅如此,Java以17.856%超过第二名C语言的8.726%两倍以上,其实,这一现象是十分反常的,因为,在前几年, ...

  8. ManageEngine卓豪 IT管理峰会圆满结束

  9. syslog、日志服务器安装、卸载详解、如何安装和卸载EventLog Analyzer

  10. 绝对强大的三个linux指令: ar, nm, objdump

    前言如果普通编程不需要了解这些东西,如果想精确控制你的对象文件的格式或者你想查看一下文件对象里的内容以便作出某种判断,刚你可以看一下下面的工具:objdump, nm, ar.当然,本文不可能非常详细 ...