题目链接:https://leetcode.com/problems/reverse-bits/description/

题目大意:将数值的二进制反转。

法一(借鉴):由于是无符号32位整型,当二进制反转后会得到一个32位的整型,eclipse中已经无法正常显示了,但是转为二进制还是正确的。至于为什么我也不知道。代码如下(耗时2ms):

     public int reverseBits(int n) {
int res = 0;
for(int i = 0; i <= 31; i++) {
//1010->tmp=0,n=101,res=00,
int tmp = n & 1;//取最后一位
n = n >> 1;//右移
res = res<<1;//结果左移一位
res = res | tmp;//将最后一位加到结果左移的那个位上去,也就是最低位上
}
// System.out.println(Integer.toBinaryString(res));
//在eclipse里会显示错误,因为res已经移位形成了32位的数,已经无法正常显示了
//但是当转为二进制的时候是正常的
return res;
}

其他的解法:http://blog.csdn.net/feliciafay/article/details/44536827

190.Reverse Bits---位运算的更多相关文章

  1. Leetcode 190 Reverse Bits 位运算

    反转二进制 class Solution { public: uint32_t reverseBits(uint32_t n) { uint32_t ans = ; ; i<; ++i,n &g ...

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

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

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

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

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

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

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

  7. 【LeetCode】190. Reverse Bits 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 二进制字符串翻转 位运算 日期 题目地址:https://le ...

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

  10. 【一天一道Leetcode】#190.Reverse Bits

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 我的个人博客已创建,欢迎大家持续关注! 一天一道le ...

随机推荐

  1. BZOJ 1216 操作系统(堆)

    用堆模拟题目中的操作即可. # include <cstdio> # include <cstring> # include <cstdlib> # include ...

  2. BZOJ4921 互质序列

    即求删掉一个子序列的gcd之和.注意到前后缀gcd的变化次数都是log级的,于是暴力枚举前缀gcd和后缀gcd即可. #include<iostream> #include<cstd ...

  3. BZOJ4883 棋盘上的守卫(环套树+最小生成树)

    容易想到网络流之类的东西,虽然范围看起来不太可做,不过这提供了一种想法,即将行列分别看做点.那么我们需要找一种连n+m条边的方案,使得可以从每条边中选一个点以覆盖所有点.显然每个点至少要连一条边.于是 ...

  4. shell的uniq命令

    uniq 命令用于检查及删除文本文件中重复出现的行列,一般与 sort 命令结合使用. uniq 可检查文本文件中重复出现的行列. 命令语法: uniq [-c/d/D/u/i] [-f Fields ...

  5. Django字段类型介绍

    一,通过makemigrations,migrate来修改数据, 1.Django的ORM支持修改表结构,修改完以后,执行 python manage.py makemigrations  pytho ...

  6. Mysql(一) 基本操作

    一.介绍 1.数据库 数据库,通俗的讲,即为存储数据的“仓库”.不过,数据库不仅只是存储,还对所存储的数据做相应的管理,例如,访问权限,安全性,并发操作,数据的备份与恢复,日志等.实际上,我们所提及的 ...

  7. webpack 4.x使用总结

    1.webpack 全局安装 npm install -g webpack 2.创建项目 比如文件夹是webpackdemo cd到webpackdemo文件夹下,执行: npm init 和 npm ...

  8. Codeforces 895.C Square Subsets

    C. Square Subsets time limit per test 4 seconds memory limit per test 256 megabytes input standard i ...

  9. 电商网站中价格的精确计算(使用BigDecimal进行精确运算(实现加减乘除运算))

    使用BigDecimal的String的构造器.商业计算中,使用bigdecimal的String构造器,一定要用. 重要的事情说三遍: 商业计算中,使用bigdecimal的String构造器! 商 ...

  10. MySQL建表时列名同保留字重复问题解决办法

    建表时遇到遇到属性名同MySQL的保留字相同导致不能建表的问题,如下SQL语句: CREATE TABLE TBL_ACCOUNT_FROZEN_RECORD ( ID BIGINT NOT NULL ...