3v4l.org

run code in 500+ PHP versions simultaneously
<?php function reduce($bits, $prefix, $value = '0') { if (strlen($bits) == 1) { // a single bit can be flipped as needed return array($prefix . ($bits[0] == '0' ? '1' : '0')); } if ($bits[0] == $value) { // nothing to do with this bit, flip the remainder $prefix .= $bits[0]; return reduce(substr($bits, 1), $prefix); } // need to convert balance of string to 1 followed by 0's $prefix .= $bits[0]; $steps = reduce(substr($bits, 1), $prefix, '1'); // now we can flip this bit $prefix = substr($prefix, 0, -1) . ($bits[0] == '0' ? '1' : '0'); $steps[] = $prefix . str_pad('1', strlen($bits) - 1, '0'); // now reduce the new string to 0 $steps = array_merge($steps, reduce(str_pad('1', strlen($bits) - 1, '0'), $prefix)); return $steps; } foreach (array(8, 115) as $i) { $bin = decbin($i); $steps = array_merge(array($bin), reduce($bin, '')); echo "$i ($bin) takes " . (count($steps) - 1) . " steps\n"; print_r($steps); }
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 1, Position 2 = 29
Branch analysis from position: 1
2 jumps found. (Code = 78) Position 1 = 2, Position 2 = 29
Branch analysis from position: 2
1 jumps found. (Code = 42) Position 1 = 1
Branch analysis from position: 1
Branch analysis from position: 29
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 29
filename:       /in/TJZuB
function name:  (null)
number of ops:  31
compiled vars:  !0 = $i, !1 = $bin, !2 = $steps
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   24     0  E > > FE_RESET_R                                           $3      <array>, ->29
          1    > > FE_FETCH_R                                                   $3, !0, ->29
   25     2    >   INIT_FCALL                                                   'decbin'
          3        SEND_VAR                                                     !0
          4        DO_ICALL                                             $4      
          5        ASSIGN                                                       !1, $4
   26     6        INIT_FCALL                                                   'array_merge'
          7        INIT_ARRAY                                           ~6      !1
          8        SEND_VAL                                                     ~6
          9        INIT_FCALL                                                   'reduce'
         10        SEND_VAR                                                     !1
         11        SEND_VAL                                                     ''
         12        DO_FCALL                                          0  $7      
         13        SEND_VAR                                                     $7
         14        DO_ICALL                                             $8      
         15        ASSIGN                                                       !2, $8
   27    16        ROPE_INIT                                         4  ~11     !0
         17        ROPE_ADD                                          1  ~11     ~11, '+%28'
         18        ROPE_ADD                                          2  ~11     ~11, !1
         19        ROPE_END                                          3  ~10     ~11, '%29+takes+'
         20        COUNT                                                ~13     !2
         21        SUB                                                  ~14     ~13, 1
         22        CONCAT                                               ~15     ~10, ~14
         23        CONCAT                                               ~16     ~15, '+steps%0A'
         24        ECHO                                                         ~16
   28    25        INIT_FCALL                                                   'print_r'
         26        SEND_VAR                                                     !2
         27        DO_ICALL                                                     
   24    28      > JMP                                                          ->1
         29    >   FE_FREE                                                      $3
   29    30      > RETURN                                                       1

Function reduce:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 6, Position 2 = 15
Branch analysis from position: 6
2 jumps found. (Code = 43) Position 1 = 9, Position 2 = 11
Branch analysis from position: 9
1 jumps found. (Code = 42) Position 1 = 12
Branch analysis from position: 12
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 11
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 15
2 jumps found. (Code = 43) Position 1 = 18, Position 2 = 26
Branch analysis from position: 18
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 26
2 jumps found. (Code = 43) Position 1 = 40, Position 2 = 42
Branch analysis from position: 40
1 jumps found. (Code = 42) Position 1 = 43
Branch analysis from position: 43
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 42
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/TJZuB
function name:  reduce
number of ops:  73
compiled vars:  !0 = $bits, !1 = $prefix, !2 = $value, !3 = $steps
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
    3     0  E >   RECV                                                 !0      
          1        RECV                                                 !1      
          2        RECV_INIT                                            !2      '0'
    4     3        STRLEN                                               ~4      !0
          4        IS_EQUAL                                                     ~4, 1
          5      > JMPZ                                                         ~5, ->15
    6     6    >   FETCH_DIM_R                                          ~6      !0, 0
          7        IS_EQUAL                                                     ~6, '0'
          8      > JMPZ                                                         ~7, ->11
          9    >   QM_ASSIGN                                            ~8      '1'
         10      > JMP                                                          ->12
         11    >   QM_ASSIGN                                            ~8      '0'
         12    >   CONCAT                                               ~9      !1, ~8
         13        INIT_ARRAY                                           ~10     ~9
         14      > RETURN                                                       ~10
    8    15    >   FETCH_DIM_R                                          ~11     !0, 0
         16        IS_EQUAL                                                     !2, ~11
         17      > JMPZ                                                         ~12, ->26
   10    18    >   FETCH_DIM_R                                          ~13     !0, 0
         19        ASSIGN_OP                                         8          !1, ~13
   11    20        INIT_FCALL_BY_NAME                                           'reduce'
         21        FRAMELESS_ICALL_2                substr              ~15     !0, 1
         22        SEND_VAL_EX                                                  ~15
         23        SEND_VAR_EX                                                  !1
         24        DO_FCALL                                          0  $16     
         25      > RETURN                                                       $16
   14    26    >   FETCH_DIM_R                                          ~17     !0, 0
         27        ASSIGN_OP                                         8          !1, ~17
   15    28        INIT_FCALL_BY_NAME                                           'reduce'
         29        FRAMELESS_ICALL_2                substr              ~19     !0, 1
         30        SEND_VAL_EX                                                  ~19
         31        SEND_VAR_EX                                                  !1
         32        SEND_VAL_EX                                                  '1'
         33        DO_FCALL                                          0  $20     
         34        ASSIGN                                                       !3, $20
   17    35        FRAMELESS_ICALL_3                substr              ~22     !1, 0
         36        OP_DATA                                                      -1
         37        FETCH_DIM_R                                          ~23     !0, 0
         38        IS_EQUAL                                                     ~23, '0'
         39      > JMPZ                                                         ~24, ->42
         40    >   QM_ASSIGN                                            ~25     '1'
         41      > JMP                                                          ->43
         42    >   QM_ASSIGN                                            ~25     '0'
         43    >   CONCAT                                               ~26     ~22, ~25
         44        ASSIGN                                                       !1, ~26
   18    45        INIT_FCALL                                                   'str_pad'
         46        SEND_VAL                                                     '1'
         47        STRLEN                                               ~29     !0
         48        SUB                                                  ~30     ~29, 1
         49        SEND_VAL                                                     ~30
         50        SEND_VAL                                                     '0'
         51        DO_ICALL                                             $31     
         52        CONCAT                                               ~32     !1, $31
         53        ASSIGN_DIM                                                   !3
         54        OP_DATA                                                      ~32
   20    55        INIT_FCALL                                                   'array_merge'
         56        SEND_VAR                                                     !3
         57        INIT_FCALL_BY_NAME                                           'reduce'
         58        INIT_FCALL                                                   'str_pad'
         59        SEND_VAL                                                     '1'
         60        STRLEN                                               ~33     !0
         61        SUB                                                  ~34     ~33, 1
         62        SEND_VAL                                                     ~34
         63        SEND_VAL                                                     '0'
         64        DO_ICALL                                             $35     
         65        SEND_VAR_NO_REF_EX                                           $35
         66        SEND_VAR_EX                                                  !1
         67        DO_FCALL                                          0  $36     
         68        SEND_VAR                                                     $36
         69        DO_ICALL                                             $37     
         70        ASSIGN                                                       !3, $37
   21    71      > RETURN                                                       !3
   22    72*     > RETURN                                                       null

End of function reduce

Generated using Vulcan Logic Dumper, using php 8.5.0


preferences:
167.04 ms | 2769 KiB | 18 Q