Added Gray code algorithm