Last active
February 25, 2016 18:36
-
-
Save jabranr/7406258 to your computer and use it in GitHub Desktop.
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/)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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