Java solution only use shift


  • 1
    O
    public class Solution {
        // you need treat n as an unsigned value
        public int reverseBits(int n) {
            int reverse = 0;
            for(int i = 0; i < 32; i++){
                int temp = n << i;
                temp = temp >>> 31 << i;
                reverse += temp;
            }
            return reverse;
        }
    }

Log in to reply
 

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