reverse bits java

Solutions on MaxInterview for reverse bits java by the best coders in the world

showing results for - "reverse bits java"
Paddy
23 Jan 2019
1public class MyClass {
2
3    // function to reverse bits of a number
4    public static int reverseBits(int n)  {
5        int rev = 0;
6        while (n > 0) {
7            rev <<= 1;
8            if ((int)(n & 1) == 1)
9                rev ^= 1;
10            n >>= 1;
11        }
12        return rev;
13    }
14 
15    public static void main(String[] args)  {
16        int n = 11;
17        System.out.println(reverseBits(n));
18    }
19}