- var_dump: documentation ( source)
- decbin: documentation ( source)
- strrev: documentation ( source)
- pow: documentation ( source)
<?php
$val = 12;
$bin_val = 1 << ($val -1);
var_dump($bin_val);
var_dump(pow(2, $val - 1));
$val = 1 >> ($val - 1);
var_dump($val);
$binary_string = decbin($bin_val );
// This command test the string with binary values, returning an array with the positions where we have '1'.
$positions = StringHelper::strPosAll(strrev($binary_string), '1');
if (is_array($positions)) {
// We have to add 1 position to fix the "starts with 0 of arrays".
foreach ($positions as $key => $value) {
$positions[$key] = ($value + 1);
}
} else {
$positions = array();
}
var_dump($positions);