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; }
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 4, Position 2 = 8
Branch analysis from position: 4
2 jumps found. (Code = 78) Position 1 = 5, Position 2 = 8
Branch analysis from position: 5
1 jumps found. (Code = 42) Position 1 = 4
Branch analysis from position: 4
Branch analysis from position: 8
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 8
filename:       /in/aSJJh
function name:  (null)
number of ops:  10
compiled vars:  !0 = $code
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   28     0  E >   INIT_FCALL                                               'iterate_gray'
          1        SEND_VAL                                                 8
          2        DO_FCALL                                      0  $1      
          3      > FE_RESET_R                                       $2      $1, ->8
          4    > > FE_FETCH_R                                               $2, !0, ->8
   29     5    >   CONCAT                                           ~3      !0, '%0A'
          6        ECHO                                                     ~3
   28     7      > JMP                                                      ->4
          8    >   FE_FREE                                                  $2
   30     9      > RETURN                                                   1

Function gray_to_binary:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 21
Branch analysis from position: 21
2 jumps found. (Code = 44) Position 1 = 23, Position 2 = 12
Branch analysis from position: 23
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 12
2 jumps found. (Code = 44) Position 1 = 23, Position 2 = 12
Branch analysis from position: 23
Branch analysis from position: 12
filename:       /in/aSJJh
function name:  gray_to_binary
number of ops:  25
compiled vars:  !0 = $value, !1 = $dec, !2 = $bits, !3 = $i
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   RECV                                             !0      
    4     1        ASSIGN                                                   !1, 0
    5     2        INIT_FCALL                                               'floor'
          3        INIT_FCALL                                               'log'
          4        SEND_VAR                                                 !0
          5        SEND_VAL                                                 2
          6        DO_ICALL                                         $5      
          7        SEND_VAR                                                 $5
          8        DO_ICALL                                         $6      
          9        ASSIGN                                                   !2, $6
    6    10        ASSIGN                                                   !3, !2
         11      > JMP                                                      ->21
    7    12    >   ADD                                              ~9      !3, 1
         13        SR                                               ~10     !1, ~9
         14        SR                                               ~11     !0, !3
         15        BW_XOR                                           ~12     ~10, ~11
         16        BW_AND                                           ~13     ~12, 1
         17        SL                                               ~14     ~13, !3
         18        BW_OR                                            ~15     !1, ~14
         19        ASSIGN                                                   !1, ~15
    6    20        PRE_DEC                                                  !3
         21    >   IS_SMALLER_OR_EQUAL                                      0, !3
         22      > JMPNZ                                                    ~18, ->12
    9    23    > > RETURN                                                   !1
   10    24*     > RETURN                                                   null

End of function gray_to_binary

Function iterate_gray:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 30
Branch analysis from position: 30
2 jumps found. (Code = 44) Position 1 = 32, Position 2 = 14
Branch analysis from position: 32
1 jumps found. (Code = 161) Position 1 = -2
Branch analysis from position: 14
1 jumps found. (Code = 42) Position 1 = 17
Branch analysis from position: 17
2 jumps found. (Code = 44) Position 1 = 20, Position 2 = 16
Branch analysis from position: 20
2 jumps found. (Code = 44) Position 1 = 32, Position 2 = 14
Branch analysis from position: 32
Branch analysis from position: 14
Branch analysis from position: 16
2 jumps found. (Code = 44) Position 1 = 20, Position 2 = 16
Branch analysis from position: 20
Branch analysis from position: 16
filename:       /in/aSJJh
function name:  iterate_gray
number of ops:  33
compiled vars:  !0 = $value, !1 = $code, !2 = $len, !3 = $count, !4 = $xor
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   12     0  E >   RECV                                             !0      
          1        GENERATOR_CREATE                                         
   14     2        INIT_FCALL                                               'decbin'
          3        SEND_VAR                                                 !0
          4        DO_ICALL                                         $5      
          5        ASSIGN                                                   !1, $5
   15     6        YIELD                                                    !1
   16     7        STRLEN                                           ~8      !1
          8        ASSIGN                                                   !2, ~8
   17     9        INIT_FCALL                                               'gray_to_binary'
         10        SEND_VAR                                                 !0
         11        DO_FCALL                                      0  $10     
         12        ASSIGN                                                   !3, $10
   18    13      > JMP                                                      ->30
   20    14    >   ASSIGN                                                   !4, 1
   21    15      > JMP                                                      ->17
         16    >   ASSIGN_OP                                     6          !4, 1
         17    >   BW_AND                                           ~14     !3, !4
         18        IS_EQUAL                                                 ~14, 0
         19      > JMPNZ                                                    ~15, ->16
   22    20    >   ASSIGN_OP                                    11          !0, !4
   23    21        INIT_FCALL                                               'sprintf'
         22        ROPE_INIT                                     3  ~18     '%250'
         23        ROPE_ADD                                      1  ~18     ~18, !2
         24        ROPE_END                                      2  ~17     ~18, 'b'
         25        SEND_VAL                                                 ~17
         26        SEND_VAR                                                 !0
         27        DO_ICALL                                         $20     
         28        YIELD                                                    $20
   24    29        PRE_DEC                                                  !3
   18    30    >   IS_SMALLER                                               0, !3
         31      > JMPNZ                                                    ~23, ->14
   26    32    > > GENERATOR_RETURN                                         

End of function iterate_gray

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
141.29 ms | 1441 KiB | 19 Q