3v4l.org

run code in 300+ PHP versions simultaneously
<?php function gray_to_binary($value) { $dec = 0; $bits = floor(log($value, 2)); for ($i = $bits; $i >= 0; $i--) { $dec = $dec | (((($dec >> ($i + 1)) ^ ($value >> $i)) & 1) << $i); } return $dec; } function iterate_gray($value) { // get the equivalent starting binary value $code = decbin($value); yield $code; $len = strlen($code); $count = gray_to_binary($value); while ($count > 0) { // flip the bit which corresponds to the least signficiant 1 bit in $count $xor = 1; while (($count & $xor) == 0) $xor <<= 1; $value ^= $xor; yield sprintf("%0{$len}b", $value); $count--; } } foreach (iterate_gray(8) as $code) { echo $code . PHP_EOL; }
Output for 7.2.0 - 7.2.34, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.29, 8.2.0 - 8.2.23, 8.3.0 - 8.3.11
1000 1001 1011 1010 1110 1111 1101 1100 0100 0101 0111 0110 0010 0011 0001 0000

preferences:
77.98 ms | 407 KiB | 5 Q