3v4l.org

run code in 500+ PHP versions simultaneously
<?php function reduce($bits, $value = '0') { if (strlen($bits) == 1) { // a single bit can be flipped as needed return ($bits[0] == $value) ? 0 : 1; } if ($bits[0] == $value) { // nothing to do with this bit, flip the remainder return reduce(substr($bits, 1)); } // need to convert balance of string to 1 followed by 0's // then we can flip this bit, and then reduce the new string to 0 return reduce(substr($bits, 1), '1') + 1 + reduce(str_pad('1', strlen($bits) - 1, '0')); } for ($i = 0; $i < 128; $i++) { echo "$i (" . decbin($i) . ") takes " . reduce(decbin($i)) . " steps\n"; }
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 19
Branch analysis from position: 19
2 jumps found. (Code = 44) Position 1 = 21, Position 2 = 2
Branch analysis from position: 21
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 2
2 jumps found. (Code = 44) Position 1 = 21, Position 2 = 2
Branch analysis from position: 21
Branch analysis from position: 2
filename:       /in/colSe
function name:  (null)
number of ops:  22
compiled vars:  !0 = $i
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   17     0  E >   ASSIGN                                                       !0, 0
          1      > JMP                                                          ->19
   18     2    >   NOP                                                          
          3        FAST_CONCAT                                          ~2      !0, '+%28'
          4        INIT_FCALL                                                   'decbin'
          5        SEND_VAR                                                     !0
          6        DO_ICALL                                             $3      
          7        CONCAT                                               ~4      ~2, $3
          8        CONCAT                                               ~5      ~4, '%29+takes+'
          9        INIT_FCALL                                                   'reduce'
         10        INIT_FCALL                                                   'decbin'
         11        SEND_VAR                                                     !0
         12        DO_ICALL                                             $6      
         13        SEND_VAR                                                     $6
         14        DO_FCALL                                          0  $7      
         15        CONCAT                                               ~8      ~5, $7
         16        CONCAT                                               ~9      ~8, '+steps%0A'
         17        ECHO                                                         ~9
   17    18        PRE_INC                                                      !0
         19    >   IS_SMALLER                                                   !0, 128
         20      > JMPNZ                                                        ~11, ->2
   19    21    > > RETURN                                                       1

Function reduce:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 5, Position 2 = 12
Branch analysis from position: 5
2 jumps found. (Code = 43) Position 1 = 8, Position 2 = 10
Branch analysis from position: 8
1 jumps found. (Code = 42) Position 1 = 11
Branch analysis from position: 11
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 10
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 12
2 jumps found. (Code = 43) Position 1 = 15, Position 2 = 20
Branch analysis from position: 15
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 20
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/colSe
function name:  reduce
number of ops:  39
compiled vars:  !0 = $bits, !1 = $value
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
    3     0  E >   RECV                                                 !0      
          1        RECV_INIT                                            !1      '0'
    4     2        STRLEN                                               ~2      !0
          3        IS_EQUAL                                                     ~2, 1
          4      > JMPZ                                                         ~3, ->12
    6     5    >   FETCH_DIM_R                                          ~4      !0, 0
          6        IS_EQUAL                                                     !1, ~4
          7      > JMPZ                                                         ~5, ->10
          8    >   QM_ASSIGN                                            ~6      0
          9      > JMP                                                          ->11
         10    >   QM_ASSIGN                                            ~6      1
         11    > > RETURN                                                       ~6
    8    12    >   FETCH_DIM_R                                          ~7      !0, 0
         13        IS_EQUAL                                                     !1, ~7
         14      > JMPZ                                                         ~8, ->20
   10    15    >   INIT_FCALL_BY_NAME                                           'reduce'
         16        FRAMELESS_ICALL_2                substr              ~9      !0, 1
         17        SEND_VAL_EX                                                  ~9
         18        DO_FCALL                                          0  $10     
         19      > RETURN                                                       $10
   14    20    >   INIT_FCALL_BY_NAME                                           'reduce'
         21        FRAMELESS_ICALL_2                substr              ~11     !0, 1
         22        SEND_VAL_EX                                                  ~11
         23        SEND_VAL_EX                                                  '1'
         24        DO_FCALL                                          0  $12     
         25        ADD                                                  ~13     $12, 1
         26        INIT_FCALL_BY_NAME                                           'reduce'
         27        INIT_FCALL                                                   'str_pad'
         28        SEND_VAL                                                     '1'
         29        STRLEN                                               ~14     !0
         30        SUB                                                  ~15     ~14, 1
         31        SEND_VAL                                                     ~15
         32        SEND_VAL                                                     '0'
         33        DO_ICALL                                             $16     
         34        SEND_VAR_NO_REF_EX                                           $16
         35        DO_FCALL                                          0  $17     
         36        ADD                                                  ~18     ~13, $17
         37      > RETURN                                                       ~18
   15    38*     > RETURN                                                       null

End of function reduce

Generated using Vulcan Logic Dumper, using php 8.5.0


preferences:
185.23 ms | 3142 KiB | 16 Q