# Print 0-255 in grey code [duplicate]

Gray Code is a number system where only 1 bit of the number changes from one number to the next number. Print the 8 bit Gray Code values of 0 to 255.

Shortest Code wins!

Start of output:

 0 00000000
1 00000001
2 00000011
3 00000010
4 00000110
5 00000111
6 00000101
7 00000100
...


You only have to print the Gray Code in binary. The decimal numbers in the example are just for reference.

• Are we required to print the canonical Grey code, or just any 8-bit Grey code will do? – John Dvorak Feb 16 '17 at 3:40
• i'm probably missing something, but don't two bits (the first and last) change from 0 to 1? – Maltysen Feb 16 '17 at 3:42
• @Maltysen that would be a typo. Good catch. – John Dvorak Feb 16 '17 at 3:44
• in binary is a tad unclear. Does it have to be ones and zeroes, one per line, and padded to 8 bits (as in the example) or is the output format more liberal (list of strings, list of arrays, no left-padding, etc.)? Also, you say print. Does that mean returning the output from a function is not allowed? – Dennis Feb 16 '17 at 3:59
• Is the first digit on the second row supposed to be a 1? – Albert Renshaw Feb 16 '17 at 4:29

# Ruby, 48 33 Bytes

15 Bytes saved thanks to G B

256.times{|i|puts "%08b"%(i^i/2)}

• You could use string interpolation to make it shorter: "%08b"%x is the same as b.to_i(2).rjust(8,?0) – G B Feb 16 '17 at 8:33
• And i^(i>>1) is the same as i^i/2 – G B Feb 16 '17 at 8:34

# C, 89 bytes

(Note: Leading Zeros Omitted from output)

q;main(a){a--;for(q=a^(a>>1);q;)printf("%c%i",8207,q%2),q/=2;puts("");a<256?main(a+2):0;}


I use a very neat trick in this one:

It converts a decimal to gray code (base 10), then convert that to binary which will print the grey number in reverse order. But before each time I output the mirrored grey number, I print U+200F or char(8207) which is a Right-To-Left marker character which causes output to be mirrored (and on the right side of my screen). Mirroring my backwards grey number outputs, thus making them forward, proper grey numbers (At least to human eyes, to the computer they are still backwards).

Notice the numbers are different in the LTR output, they are mirrored grey numbers.

(Note: Leading Zeros Omitted from output)

# Jelly, 10 bytes

⁹ḶH+^ḶBḊ€Y


Try it online!

# PHP, 43 40 bytes

for(;$i<256;$i++)printf("%b\n",$i^$i/2);


+2 bytes for padded: replace %b with %08b.

Thanks @user63956 for reminding me of printf.

• You can use printf("%08b\n", ...) in the padded version and inline the increment: $i/2^$i++. – user63956 Feb 16 '17 at 5:57
• @user63956 Thanks for reminding me of printf. I already tried that increment; it should work, but it doesn´t. – Titus Feb 16 '17 at 6:04

# Python, 53 bytes

for i in range(256): print(bin(i^(i>1))[2:].zfill(8))

• If you switch to python2 you can swap i^(i>1) to i^i/2` because python2's division operator automatically rounds down. I was just in the middle of writing an answer when xnor closed it, it came to 49 bytes. – sagiksp Feb 16 '17 at 6:30