3v4l.org

run code in 500+ PHP versions simultaneously
<?php function gray_code($bits) { if ($bits == 1) { return array('0', '1'); } else { $codes = gray_code($bits - 1); return array_merge(array_map(function ($v) { return '0' . $v; }, $codes), array_map(function ($v) { return '1' . $v; }, array_reverse($codes)) ); } } $value = 115; $bin = decbin($value); // get sufficient gray codes to cover the input $gray_codes = gray_code(strlen($bin)); $codes = array_flip($gray_codes); echo "$bin takes {$codes[$bin]} steps to reduce to 0\n"; // echo the steps for ($i = $codes[$bin]; $i >= 0; $i--) { echo $gray_codes[$i] . PHP_EOL; }
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 27
Branch analysis from position: 27
2 jumps found. (Code = 44) Position 1 = 29, Position 2 = 23
Branch analysis from position: 29
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 23
2 jumps found. (Code = 44) Position 1 = 29, Position 2 = 23
Branch analysis from position: 29
Branch analysis from position: 23
filename:       /in/9I40K
function name:  (null)
number of ops:  30
compiled vars:  !0 = $value, !1 = $bin, !2 = $gray_codes, !3 = $codes, !4 = $i
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   15     0  E >   ASSIGN                                                       !0, 115
   16     1        INIT_FCALL                                                   'decbin'
          2        SEND_VAR                                                     !0
          3        DO_ICALL                                             $6      
          4        ASSIGN                                                       !1, $6
   18     5        INIT_FCALL                                                   'gray_code'
          6        STRLEN                                               ~8      !1
          7        SEND_VAL                                                     ~8
          8        DO_FCALL                                          0  $9      
          9        ASSIGN                                                       !2, $9
   19    10        INIT_FCALL                                                   'array_flip'
         11        SEND_VAR                                                     !2
         12        DO_ICALL                                             $11     
         13        ASSIGN                                                       !3, $11
   20    14        ROPE_INIT                                         4  ~15     !1
         15        ROPE_ADD                                          1  ~15     ~15, '+takes+'
         16        FETCH_DIM_R                                          ~13     !3, !1
         17        ROPE_ADD                                          2  ~15     ~15, ~13
         18        ROPE_END                                          3  ~14     ~15, '+steps+to+reduce+to+0%0A'
         19        ECHO                                                         ~14
   22    20        FETCH_DIM_R                                          ~17     !3, !1
         21        ASSIGN                                                       !4, ~17
         22      > JMP                                                          ->27
   23    23    >   FETCH_DIM_R                                          ~19     !2, !4
         24        CONCAT                                               ~20     ~19, '%0A'
         25        ECHO                                                         ~20
   22    26        PRE_DEC                                                      !4
         27    >   IS_SMALLER_OR_EQUAL                                          0, !4
         28      > JMPNZ                                                        ~22, ->23
   24    29    > > RETURN                                                       1

Function gray_code:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 3, Position 2 = 5
Branch analysis from position: 3
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 5
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/9I40K
function name:  gray_code
number of ops:  29
compiled vars:  !0 = $bits, !1 = $codes
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
    3     0  E >   RECV                                                 !0      
    4     1        IS_EQUAL                                                     !0, 1
          2      > JMPZ                                                         ~2, ->5
    5     3    > > RETURN                                                       <array>
    4     4*       JMP                                                          ->28
    8     5    >   INIT_FCALL_BY_NAME                                           'gray_code'
          6        SUB                                                  ~3      !0, 1
          7        SEND_VAL_EX                                                  ~3
          8        DO_FCALL                                          0  $4      
          9        ASSIGN                                                       !1, $4
    9    10        INIT_FCALL                                                   'array_merge'
         11        INIT_FCALL                                                   'array_map'
         12        DECLARE_LAMBDA_FUNCTION                              ~6      [0]
         13        SEND_VAL                                                     ~6
         14        SEND_VAR                                                     !1
         15        DO_ICALL                                             $7      
         16        SEND_VAR                                                     $7
   10    17        INIT_FCALL                                                   'array_map'
         18        DECLARE_LAMBDA_FUNCTION                              ~8      [1]
         19        SEND_VAL                                                     ~8
         20        INIT_FCALL                                                   'array_reverse'
         21        SEND_VAR                                                     !1
         22        DO_ICALL                                             $9      
         23        SEND_VAR                                                     $9
         24        DO_ICALL                                             $10     
         25        SEND_VAR                                                     $10
    9    26        DO_ICALL                                             $11     
   10    27      > RETURN                                                       $11
   13    28*     > RETURN                                                       null


Dynamic Functions:
Dynamic Function 0
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/9I40K
function name:  {closure:gray_code():9}
number of ops:  4
compiled vars:  !0 = $v
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
    9     0  E >   RECV                                                 !0      
          1        CONCAT                                               ~1      '0', !0
          2      > RETURN                                                       ~1
          3*     > RETURN                                                       null

End of Dynamic Function 0

Dynamic Function 1
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/9I40K
function name:  {closure:gray_code():10}
number of ops:  4
compiled vars:  !0 = $v
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   10     0  E >   RECV                                                 !0      
          1        CONCAT                                               ~1      '1', !0
          2      > RETURN                                                       ~1
          3*     > RETURN                                                       null

End of Dynamic Function 1

End of function gray_code

Generated using Vulcan Logic Dumper, using php 8.5.0


preferences:
170.64 ms | 2355 KiB | 19 Q