Leetcode#190Reverse 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).

無符號×××數據的反轉,一般採用位操作,左移<<,右移>>,且&,或|等操作

public class Solution {

    // you need treat n as an unsigned value

    public int reverseBits(int n) {

        int m=n&1;

        for(int i=1;i<32;i++)

        {

            m=m<<1;

            n=n>>1;

            m=m|(n&1);

        }

        return m;

    }

}


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