Mid
From PDF
Coding
C# Coding Interview
Reverse the bits of a number?
public uint ReverseBits(uint n) {
uint result = 0;
for (int i = 0; i < 32; i++) {
result <<= 1;
result |= (n & 1);
n >>= 1;
}
return result;
}
Explanation:
Iteratively take least significant bit of n, add it to result’s LSB, shift both accordingly.
Follow on:
Follow on:
Share this Q&A
Share preview image: https://www.toolliyo.com/images/toolliyo-logo.png