Skip to content

Instantly share code, notes, and snippets.

@jabranr
Last active February 25, 2016 18:36
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jabranr/7406258 to your computer and use it in GitHub Desktop.
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/)
<?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