Add solution 2 with bit manipulation.