3v4l.org

run code in 300+ PHP versions simultaneously
<?php function roundup_pow2(int $v, int $p2): int { $p2 = 2 ** $p2 - 1; return ($v + $p2) & ~$p2; } /* examples: round up to the multiple of 32 (2^5) */ echo roundup_pow2(31, 5), PHP_EOL; echo roundup_pow2(32, 5), PHP_EOL; echo roundup_pow2(33, 5), PHP_EOL; /* and here's how you align your strings using it */ $test = "12345"; /* let's align this to 4 chars boundary */ echo str_pad($test, roundup_pow2(strlen($test), 2), "0", STR_PAD_LEFT), PHP_EOL;
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/SiTnY
function name:  (null)
number of ops:  33
compiled vars:  !0 = $test
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   10     0  E >   INIT_FCALL                                               'roundup_pow2'
          1        SEND_VAL                                                 31
          2        SEND_VAL                                                 5
          3        DO_FCALL                                      0  $1      
          4        ECHO                                                     $1
          5        ECHO                                                     '%0A'
   11     6        INIT_FCALL                                               'roundup_pow2'
          7        SEND_VAL                                                 32
          8        SEND_VAL                                                 5
          9        DO_FCALL                                      0  $2      
         10        ECHO                                                     $2
         11        ECHO                                                     '%0A'
   12    12        INIT_FCALL                                               'roundup_pow2'
         13        SEND_VAL                                                 33
         14        SEND_VAL                                                 5
         15        DO_FCALL                                      0  $3      
         16        ECHO                                                     $3
         17        ECHO                                                     '%0A'
   15    18        ASSIGN                                                   !0, '12345'
   16    19        INIT_FCALL                                               'str_pad'
         20        SEND_VAR                                                 !0
         21        INIT_FCALL                                               'roundup_pow2'
         22        STRLEN                                           ~5      !0
         23        SEND_VAL                                                 ~5
         24        SEND_VAL                                                 2
         25        DO_FCALL                                      0  $6      
         26        SEND_VAR                                                 $6
         27        SEND_VAL                                                 '0'
         28        SEND_VAL                                                 0
         29        DO_ICALL                                         $7      
         30        ECHO                                                     $7
         31        ECHO                                                     '%0A'
         32      > RETURN                                                   1

Function roundup_pow2:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/SiTnY
function name:  roundup_pow2
number of ops:  12
compiled vars:  !0 = $v, !1 = $p2
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   RECV                                             !0      
          1        RECV                                             !1      
    5     2        POW                                              ~2      2, !1
          3        SUB                                              ~3      ~2, 1
          4        ASSIGN                                                   !1, ~3
    6     5        ADD                                              ~5      !0, !1
          6        BW_NOT                                           ~6      !1
          7        BW_AND                                           ~7      ~5, ~6
          8        VERIFY_RETURN_TYPE                                       ~7
          9      > RETURN                                                   ~7
    7    10*       VERIFY_RETURN_TYPE                                       
         11*     > RETURN                                                   null

End of function roundup_pow2

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
165.02 ms | 1407 KiB | 19 Q