Java Solution with 4lines, 2ms

  • 1
    public int reverseBits(int n) {
        int rev = 0;
        for(int i=0;i<32;i++)
            rev |= ((n>>>i)&1)<<(31-i);
        return rev;


    ((n>>>i)&1) : pick the bit

    <<(31-i) : move to the reverse position

    finally, use OR operation to combine them

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.