My Solution in Java 2 ms


  • 0
    G
    public class Solution {
        // you need treat n as an unsigned value
        public int reverseBits(int n) {
            int rev = 0;
            int idx = 31;
            for(int i = 0; i < 32;i++, n = n>>1){
                // get bit by AND with 1
                int bit = (1 & n);
    
                rev = rev | (bit<<(idx-i));
            }
         
         return rev;   
        }
    }

Log in to reply
 

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