https://leetcode.com/problems/reverse-bits/description/
int자료형 32비트이므로 32번의 순환 + 시프트연산으로 O(1)으로 해결할 수 있을거란 생각을 했다.
아이디어까지는 좋았으나 마지막비트 누적 연산에서 오류가 있어 통과하지 못하는 케이스가 있었다.
결국 뒤집을 대상(n)의 마지막 비트를 OR 연산하면 된다는 것을 이해했다.
비트연산에 대해 더 익숙해질 수 있는 시간이였다.
public class Solution {
// you need treat n as an unsigned value
public int reverseBits(int n) {
int result = 0;
for (int i=0; i<32; i++) {
result = result << 1;
result = result | (n & 1);
n = n >> 1;
}
return result;
}
}