[InterviewBit] Reverse Bits

Reverse Bits

  • Time :
  • Space :
1
2
3
4
5
6
7
8
unsigned int Solution::reverse(unsigned int A) {
unsigned int res = 0;
for(int i = 0; i < 32; i++) {
if(A & (1<<i)) res |= 1<<(32-i-1);
}
return res;
}

Author: Song Hayoung
Link: https://songhayoung.github.io/2022/09/29/PS/interviewbit/reverse-bits/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.