Easy-题目43:190. Reverse Bits

来源:互联网 发布:边际递减效应爱情知乎 编辑:程序博客网 时间:2024/06/08 00:55

题目原文:
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).
题目大意:
对一个32位的无符号整数,翻转它的比特位。
题目分析:
用一个新数t从0开始记录n的最后一位,然后令n右移,t左移。
源码:(language:c)

uint32_t reverseBits(uint32_t n) {    int t=0;    for(int i=0;i<32;i++) {        t=t*2+n%2;        n/=2;    }    return t;}

成绩:
4ms,beats 10.63%,众数4ms,89.37%

0 0
原创粉丝点击