Numbers and bits · 12 / 15 Exercise Medium

Gray code

Objective

The Gray code orders the n-bit numbers so that two consecutive ones differ in a single bit. Value i of the sequence is obtained with i ^ (i >> 1).

Instructions

  • The input is an integer n (between 1 and 6).
  • Show the 2**n Gray codes of n bits, one per line and in binary with leading zeros, starting at i = 0.
Input
2
Output
00
01
11
10

Open the editor in full screen: the statement on the left and your code, the output and the tests on the right.

Start exercise