Cover image for Classic Interview 150 Questions P190 Reverse Bits

Classic Interview 150 Questions P190 Reverse Bits


Timeline

Timeline

2025-11-25

init

Bit manipulation

Problem:

In-place reversal is a bit troublesome; you can use a temporary variable.

12345678910111213
class Solution {    public:	int reverseBits(int n)	{		unsigned int r = 0;		for (int i = 0; i < 32; i++) {			r <<= 1;			r |= (n & 1);			n >>= 1;		}		return r;	}};
Loading comments…