How to extract all bit flags in PHP
Snippet
Example with print all flags:
<?php
$value = 1153;
function hasStaticType($num, $type) {
return ($num & $type) ? true : false;
}
$i=0;
while($type<$value){
$type = 1 << $i;
if(hasStaticType($value,$type)){
echo $type.PHP_EOL;
}
$i++;
}
Example of a function that will return all flags in an array:
<?php
function hasStaticType($num, $type) {
return ($num & $type) ? true : false;
}
function getAllFlags($value) {
$res = [];
$i = $type = 0;
while($type < $value){
$type = 1 << $i;
if(hasStaticType($value,$type)){
$res[] = $type;
}
$i++;
}
return $res;
}
/* USING */
$value = 1153;
print_r(getAllFlags($value));