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

思路:

n一直右移, ans一直左移。

class Solution {
public:
uint32_t reverseBits(uint32_t n) {
uint32_t ans = ;
for(int i = ; i < ; i++)
{
if(n & 0x0001 == )
{
n = n >> ;
ans = ans << ;
ans |= 0x0001;
}
else
{
n = n >> ;
ans = ans << ;
}
}
return ans;
}
};

大神总会有更简单的写法:

     uint32_t  reverseBits(uint32_t n) {
uint32_t result= ;
for(int i=; i<; i++)
result = (result<<) + (n>>i &); return result;
}

【leetcode】Reverse Bits(middle)的更多相关文章

  1. 【leetcode】Reverse Integer(middle)☆

    Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 总结:处理整数溢出 ...

  2. 【leetcode】Reorder List (middle)

    Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You must do thi ...

  3. 【leetcode】Next Permutation(middle)

    Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...

  4. 【leetcode】Word Break (middle)

    Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...

  5. 【LeetCode】Counting Bits(338)

    1. Description Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num ...

  6. 【leetcode】Rotate List(middle)

    Given a list, rotate the list to the right by k places, where k is non-negative. For example:Given 1 ...

  7. 【leetcode】Partition List(middle)

    Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...

  8. 【leetcode】Spiral Matrix(middle)

    Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...

  9. 【leetcode】Rotate Image(middle)

    You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...

随机推荐

  1. 笔记之Python网络数据采集

    笔记之Python网络数据采集 非原创即采集 一念清净, 烈焰成池, 一念觉醒, 方登彼岸 网络数据采集, 无非就是写一个自动化程序向网络服务器请求数据, 再对数据进行解析, 提取需要的信息 通常, ...

  2. Linux(Red Hat)-中安装Vmware Tools

    0. 会弹出 ***1.在弹出的文件夹中找到"VMwwareTools9.6.2-1688356.tar.gz",右击"解压缩到"...我解压缩到了" ...

  3. solr多条件查询(三)

    1.昨天记了一下三条件的“并且” “并且”(  &&   &&  )的情况,今天再来记一下 “并且”  “或者” 的情况. 这里的或者情况,一定要搞清楚无论有多少情况, ...

  4. 快速切换IP的批处理!

    内容如下: @echo off color 1A Title [SMART专用 IP设置V1.0] cls echo. echo SMART专用 IP设置V1.0 %date%%time% echo. ...

  5. python 爬虫1

    简单访问有道词典的翻译界面,将页面翻译功能简单呈现 import urllib.request import urllib.parse import json content = input(&quo ...

  6. ie下获取上传文件全路径

    ie下获取上传文件全路径,3.5之后的火狐是没法获取上传文件全路径的 /*获取上传文件路径*/ function getFilePath(obj) { var form = $(this).paren ...

  7. git之远程标签下载(远程分支)

    一般我们发布一个新版本到线上服务器时都会在版本库中打一个标签,这样我们可以随时查看这个打标签的版本,就是说标签其实是版本库中一个快照.git的标签与分支类似,区别是分支是可变的而标签是不可变.接下来我 ...

  8. 解决vs2010“创建或打开C++浏览数据库文件 发生错误”的问题 Microsoft SQL Server Compact 3.5

    有网友说打开vs2010安装光盘,搜索 SSCERuntime_x86-chs.msi,重新安装之.于是果断搜索,发现SSCERuntime_x86-chs.msi,另外发现一个SSCEVSTools ...

  9. 关于CSS中对IE条件注释的问题

    一.通用区分方式:IE6.IE7能识别*,标准浏览器(如FF)不能识别*:IE6能识别*,但不能识别 !important:IE7能识别*,也能识别 !important:IE8能识别\0,不能识别* ...

  10. [POJ3096]Surprising Strings

    [POJ3096]Surprising Strings 试题描述 The D-pairs of a string of letters are the ordered pairs of letters ...