Created
April 7, 2014 09:03
-
-
Save justinpage/10016995 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
| echo "\nEnter an integer for its inverted complement: "; | |
| /** | |
| * Scan input from command-line, make sure the input has value, | |
| * send input to accompanying function and output inverted compliment | |
| */ | |
| if ( 1 === fscanf(STDIN, '%d', $input)) { | |
| $comp = getIntegerComplement($input); | |
| echo "\nThe inverted compliment of $input is: $comp\n"; | |
| } | |
| /** | |
| * getIntegerComplement will take a current input, | |
| * convert the input to a string value of boolean numbers. | |
| * grab the current length of that boolean string, | |
| * save space for the exponent and decimal value. | |
| * | |
| * for loop will increment through the current string, casting each value | |
| * to an integer, and then check to see if that value is zero. | |
| * We are only concerned for falsey inverted values for proper complement. | |
| * Concatenate current decimnal value with properly raised exponents. | |
| * | |
| * @param int $input | |
| * @int $dec | |
| */ | |
| function getIntegerComplement($input) { | |
| $bin = decbin($input); | |
| $len = strlen($bin); | |
| $exp = $len - 1; | |
| $dec = null; | |
| for ($i = 0; $i < $len; $i++) { | |
| $num = (int) $bin[$i]; | |
| if(0 === $num) $dec += pow(2, $exp); | |
| $exp--; | |
| } | |
| return $dec; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment