congr / world

2 stars 1 forks source link

LeetCode : 190. Reverse Bits #461

Closed congr closed 5 years ago

congr commented 5 years ago

https://leetcode.com/problems/reverse-bits/

image

congr commented 5 years ago
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 <<= 1;
            res |= (n >> i) & 1;
            System.out.println(Integer.toBinaryString(res));
        }

        return res;
    }
}
congr commented 5 years ago

input 00000010100101000001111010011011

stdout

1
11
110
1101
11011
110110
1101100
11011001
110110010
1101100101
11011001011
110110010111
1101100101111
11011001011110
110110010111100
1101100101111000
11011001011110000
110110010111100000
1101100101111000001
11011001011110000010
110110010111100000101
1101100101111000001010
11011001011110000010100
110110010111100000101001
1101100101111000001010010
11011001011110000010100101
110110010111100000101001010
1101100101111000001010010100
11011001011110000010100101000
110110010111100000101001010000
1101100101111000001010010100000
11011001011110000010100101000000