190. Reverse Bits


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



2 代碼


public class Solution {
    // you need treat n as an unsigned value
    public int reverseBits(int n) {
		int result = 0;
		for (int i = 0; i < 31; i++) {
			result += n & 1;
			n >>>= 1;
			result <<= 1;
		}

		result += n & 1;
		n >>>= 1;

		return result;
	}

}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章