3v4l.org

run code in 300+ 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 = 23
Branch analysis from position: 15
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 23
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/colSe
function name:  reduce
number of ops:  45
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, ->23
   10    15    >   INIT_FCALL_BY_NAME                                       'reduce'
         16        INIT_FCALL                                               'substr'
         17        SEND_VAR                                                 !0
         18        SEND_VAL                                                 1
         19        DO_ICALL                                         $9      
         20        SEND_VAR_NO_REF_EX                                       $9
         21        DO_FCALL                                      0  $10     
         22      > RETURN                                                   $10
   14    23    >   INIT_FCALL_BY_NAME                                       'reduce'
         24        INIT_FCALL                                               'substr'
         25        SEND_VAR                                                 !0
         26        SEND_VAL                                                 1
         27        DO_ICALL                                         $11     
         28        SEND_VAR_NO_REF_EX                                       $11
         29        SEND_VAL_EX                                              '1'
         30        DO_FCALL                                      0  $12     
         31        ADD                                              ~13     $12, 1
         32        INIT_FCALL_BY_NAME                                       'reduce'
         33        INIT_FCALL                                               'str_pad'
         34        SEND_VAL                                                 '1'
         35        STRLEN                                           ~14     !0
         36        SUB                                              ~15     ~14, 1
         37        SEND_VAL                                                 ~15
         38        SEND_VAL                                                 '0'
         39        DO_ICALL                                         $16     
         40        SEND_VAR_NO_REF_EX                                       $16
         41        DO_FCALL                                      0  $17     
         42        ADD                                              ~18     ~13, $17
         43      > RETURN                                                   ~18
   15    44*     > RETURN                                                   null

End of function reduce

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
153.27 ms | 1436 KiB | 17 Q