[Leetcode] 190. Reverse Bits

  1. Reverse Bits

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.

Subscribe to see which companies asked this question

public class Solution {
    // you need treat n as an unsigned value
    public int reverseBits(int n) {
        int ret = 0;
        for(int i = 0; i < 32; i++){
            ret += (n & 0x1);
            if(i < 31){
                n = n>>1;
                ret = ret<<1;
            }
        }
        return ret;
    }
}
最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內(nèi)容

  • 你我之間,有多少個金正煥。面對心愛的人,說不出動聽的話,只顧在背后默默地看著她,看著她笑,便開心,看著她流淚,便心...
    微言不知意閱讀 14,740評論 3 2
  • 上午云,下午有陽光,暖和。來東京拍照,客戶的店是按摩店,很正規(guī)的那種,在鶯谷附近、上野一帶,東京傳統(tǒng)的煙花柳巷區(qū)域...
    灰與白閱讀 282評論 0 1
  • 從你的世界路過 不小心把心落在了那里 沿途翻飛的蝴蝶 勾畫出我來時的軌跡 你的眼里有日月山河 星辰璀璨 我卻只能用...
    點點娟er閱讀 195評論 0 4
  • Java的代碼注釋 作用: 1.解釋程序中某些部分的作用和功能,提高程序的可讀性。2.可以使用注釋暫時屏蔽某些語句...
    木騰小歌閱讀 212評論 0 0