Mid Coding

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:

More from C# Programming Tutorial

All questions for this course