Skip to content

190. Reverse Bits

Reverse bits of a given 32 bits unsigned integer.

Note:

  • Note that in some languages, such as Java, there is no unsigned integer type. In this case, both input and output will be given as a signed integer type. They should not affect your implementation, as the integer's internal binary representation is the same, whether it is signed or unsigned.
  • In Java, the compiler represents the signed integers using 2's complement notation. Therefore, in Example 2 above, the input represents the signed integer -3 and the output represents the signed integer -1073741825.

Example 1:

Input: n = 00000010100101000001111010011100
Output:    964176192 (00111001011110000010100101000000)
Explanation: The input binary string 00000010100101000001111010011100 represents the unsigned integer 43261596, so return 964176192 which its binary representation is 00111001011110000010100101000000.

Example 2:

Input: n = 11111111111111111111111111111101
Output:   3221225471 (10111111111111111111111111111111)
Explanation: The input binary string 11111111111111111111111111111101 represents the unsigned integer 4294967293, so return 3221225471 which its binary representation is 10111111111111111111111111111111.

Constraints:

  • The input must be a binary string of length 32

Follow up: If this function is called many times, how would you optimize it?

Solution:

public class Solution {
    // you need treat n as an unsigned value
    public int reverseBits(int n) {
        int result = 0;
        // n:     -> 
        // result:    <-
        for (int i = 0; i < 32; i++) {
            result = result << 1;    // shift all bit one position left
            // 0101 << 1    = 1010
            int bit = (n & 1); // // Extract the least significant bit of n
            result = result | bit;
            // Perform bitwise OR on result with the extracted bit 
            // and store the result back in result
            n = n >> 1;
        }
        return result;
    }
}

// TC: O(n)
// SC: O(1)

<<: Left Shift Operator

int a = 5; // binary representation is 0101
int result = a << 2; // result is 10100, which is 20 in decimal

The left shift operator << shifts the bits of its left-hand operand to the left by the number of positions specified by its right-hand operand. Vacant positions are filled with zeros.

>>: Signed Right Shift

int a = -20; // negative numbers are represented in two's complement form
int result = a >> 2; // result maintains the sign, giving -5

The signed right shift operator >> shifts the bits of its left-hand operand to the right by the number of positions specified by its right-hand operand. Vacant positions are filled with the sign bit.

>>>: Unsigned Right Shift

int a = -20;
int result = a >>> 2; // result is a large number because the sign bit is not propagated

The unsigned right shift operator >>> shifts the bits of its left-hand operand to the right by the number of positions specified by its right-hand operand. Vacant positions are filled with zeros, regardless of the sign of the initial number.