3v4l.org

run code in 300+ 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 = 29
Branch analysis from position: 18
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 29
2 jumps found. (Code = 43) Position 1 = 49, Position 2 = 51
Branch analysis from position: 49
1 jumps found. (Code = 42) Position 1 = 52
Branch analysis from position: 52
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 51
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/TJZuB
function name:  reduce
number of ops:  82
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, ->29
   10    18    >   FETCH_DIM_R                                      ~13     !0, 0
         19        ASSIGN_OP                                     8          !1, ~13
   11    20        INIT_FCALL_BY_NAME                                       'reduce'
         21        INIT_FCALL                                               'substr'
         22        SEND_VAR                                                 !0
         23        SEND_VAL                                                 1
         24        DO_ICALL                                         $15     
         25        SEND_VAR_NO_REF_EX                                       $15
         26        SEND_VAR_EX                                              !1
         27        DO_FCALL                                      0  $16     
         28      > RETURN                                                   $16
   14    29    >   FETCH_DIM_R                                      ~17     !0, 0
         30        ASSIGN_OP                                     8          !1, ~17
   15    31        INIT_FCALL_BY_NAME                                       'reduce'
         32        INIT_FCALL                                               'substr'
         33        SEND_VAR                                                 !0
         34        SEND_VAL                                                 1
         35        DO_ICALL                                         $19     
         36        SEND_VAR_NO_REF_EX                                       $19
         37        SEND_VAR_EX                                              !1
         38        SEND_VAL_EX                                              '1'
         39        DO_FCALL                                      0  $20     
         40        ASSIGN                                                   !3, $20
   17    41        INIT_FCALL                                               'substr'
         42        SEND_VAR                                                 !1
         43        SEND_VAL                                                 0
         44        SEND_VAL                                                 -1
         45        DO_ICALL                                         $22     
         46        FETCH_DIM_R                                      ~23     !0, 0
         47        IS_EQUAL                                                 ~23, '0'
         48      > JMPZ                                                     ~24, ->51
         49    >   QM_ASSIGN                                        ~25     '1'
         50      > JMP                                                      ->52
         51    >   QM_ASSIGN                                        ~25     '0'
         52    >   CONCAT                                           ~26     $22, ~25
         53        ASSIGN                                                   !1, ~26
   18    54        INIT_FCALL                                               'str_pad'
         55        SEND_VAL                                                 '1'
         56        STRLEN                                           ~29     !0
         57        SUB                                              ~30     ~29, 1
         58        SEND_VAL                                                 ~30
         59        SEND_VAL                                                 '0'
         60        DO_ICALL                                         $31     
         61        CONCAT                                           ~32     !1, $31
         62        ASSIGN_DIM                                               !3
         63        OP_DATA                                                  ~32
   20    64        INIT_FCALL                                               'array_merge'
         65        SEND_VAR                                                 !3
         66        INIT_FCALL_BY_NAME                                       'reduce'
         67        INIT_FCALL                                               'str_pad'
         68        SEND_VAL                                                 '1'
         69        STRLEN                                           ~33     !0
         70        SUB                                              ~34     ~33, 1
         71        SEND_VAL                                                 ~34
         72        SEND_VAL                                                 '0'
         73        DO_ICALL                                         $35     
         74        SEND_VAR_NO_REF_EX                                       $35
         75        SEND_VAR_EX                                              !1
         76        DO_FCALL                                      0  $36     
         77        SEND_VAR                                                 $36
         78        DO_ICALL                                         $37     
         79        ASSIGN                                                   !3, $37
   21    80      > RETURN                                                   !3
   22    81*     > RETURN                                                   null

End of function reduce

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
161.45 ms | 1442 KiB | 19 Q