2
\$\begingroup\$

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.

\$\endgroup\$
7
  • 1
    \$\begingroup\$ Are we required to print the canonical Grey code, or just any 8-bit Grey code will do? \$\endgroup\$ Feb 16, 2017 at 3:40
  • 3
    \$\begingroup\$ i'm probably missing something, but don't two bits (the first and last) change from 0 to 1? \$\endgroup\$
    – Maltysen
    Feb 16, 2017 at 3:42
  • 1
    \$\begingroup\$ @Maltysen that would be a typo. Good catch. \$\endgroup\$ Feb 16, 2017 at 3:44
  • 2
    \$\begingroup\$ 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? \$\endgroup\$
    – Dennis
    Feb 16, 2017 at 3:59
  • 1
    \$\begingroup\$ Is the first digit on the second row supposed to be a 1? \$\endgroup\$ Feb 16, 2017 at 4:29

5 Answers 5

2
\$\begingroup\$

Ruby, 48 33 Bytes

15 Bytes saved thanks to G B

256.times{|i|puts "%08b"%(i^i/2)}
\$\endgroup\$
2
  • \$\begingroup\$ You could use string interpolation to make it shorter: "%08b"%x is the same as `b.to_i(2).rjust(8,?0) \$\endgroup\$
    – G B
    Feb 16, 2017 at 8:33
  • 1
    \$\begingroup\$ And i^(i>>1) is the same as i^i/2 \$\endgroup\$
    – G B
    Feb 16, 2017 at 8:34
1
\$\begingroup\$

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. enter image description here

(Note: Leading Zeros Omitted from output)

\$\endgroup\$
1
0
\$\begingroup\$

Jelly, 10 bytes

⁹ḶH+^ḶBḊ€Y

Try it online!

\$\endgroup\$
0
\$\begingroup\$

PHP, 43 40 bytes

unpadded

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.

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

Python, 53 bytes

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

\$\endgroup\$
1
  • \$\begingroup\$ 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. \$\endgroup\$
    – sagiksp
    Feb 16, 2017 at 6:30

Not the answer you're looking for? Browse other questions tagged or ask your own question.