Overall the bit-swapping algorithm would be slightly more clear if you defined `n = 1 << 7` initially and then did `n >>= 1` in the loop.

Also, you need to handle the case where n becomes 0.