📚 Bit Reversal Algorithm
Reversing bits swaps the bit at position i with position (n-1-i):
// 8-bit reversal example:
// Input: 43 = 00101011
// Output: 212 = 11010100
function reverseBits(n, numBits) {
let result = 0;
for (let i = 0; i < numBits; i++) {
result <<= 1;
result |= (n & 1);
n >>= 1;
}
return result;
}