Share my AC Java code


  • 0
    O

    This solution works perfectly in Java, as hard coded part used int type definition in Java -- a 32-bit number.

    public class Solution {
        // you need treat n as an unsigned value
        public int reverseBits(int n) {
            // In java, int type is always signed, right shift is 
            // signed shift by default, however, we need a unsigned right
            // shift in this problem.
            n = ((n & 0xFFFF0000) >>> 16) | ((n & 0xFFFF) << 16);
            n = ((n & 0xFF00FF00) >>> 8) | ((n & 0x00FF00FF) << 8);
            n = ((n & 0xF0F0F0F0) >>> 4) | ((n & 0x0F0F0F0F) << 4);
            n = ((n & 0xCCCCCCCC) >>> 2) | ((n & 0x33333333) << 2);
            n = ((n & 0xAAAAAAAA) >>> 1) | ((n & 0x55555555) << 1);
            return n;
        }
    }

Log in to reply
 

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