Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Converts the integer to binary and outputs list of integers that are power of 2 (http://jabran.me/using-special-integers-with-bitwise-operators-for-php-rbac/)
<?php
/**
* Converts the integer to binary and outputs list of integers that are power of 2
*
* @author: Jabran Rafique <hello@jabran.me>
* @param: integer $decimal
* @return: string
*/
function get_special_binary( $decimal ) {
$binaries = '';
for ( $i = 1; $i < $decimal; $i++ ) {
$special_num = pow(2, $i);
$value = str_pad(base_convert($special_num, 10, 2), 8, '0', STR_PAD_LEFT);
$binaries .= sprintf('%s = %s %s <br />', $special_num, substr($value, 0, 4), substr($value, 4));
}
return $binaries;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment