Solve a very tricky bit manipulation puzzle by following existing solution exactly, O(n) time O(1) space