A concise Java solution


  • 1
    M
    public class Solution {
        // you need treat n as an unsigned value
        
            public int reverseBits(int n) {
           
              int res = 0;
           
              for(int i = 0; i<32;i++)
              {
                res = res*2 + ((n >> i) & 1);
            }
            return res;
        }
    }

Log in to reply
 

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