Last active
December 25, 2015 05:19
-
-
Save ankr/6923911 to your computer and use it in GitHub Desktop.
I found this one the web somewhere, I have no idea where unfortunately.
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 | |
/* | |
* Ignore the top section, | |
* it is just formatting to make output clearer. | |
*/ | |
$format = '(%1$2d = %1$04b) = (%2$2d = %2$04b)' | |
. ' %3$s (%4$2d = %4$04b)' . "\n"; | |
echo <<<EOH | |
--------- --------- -- --------- | |
result value op test | |
--------- --------- -- --------- | |
EOH; | |
/* | |
* Here are the examples. | |
*/ | |
$values = array(0, 1, 2, 4, 8); | |
$test = 1 + 4; | |
echo "\n Bitwise AND \n"; | |
foreach ($values as $value) { | |
$result = $value & $test; | |
printf($format, $result, $value, '&', $test); | |
} | |
echo "\n Bitwise Inclusive OR \n"; | |
foreach ($values as $value) { | |
$result = $value | $test; | |
printf($format, $result, $value, '|', $test); | |
} | |
echo "\n Bitwise Exclusive OR (XOR) \n"; | |
foreach ($values as $value) { | |
$result = $value ^ $test; | |
printf($format, $result, $value, '^', $test); | |
} |
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
--------- --------- -- --------- | |
result value op test | |
--------- --------- -- --------- | |
Bitwise AND | |
( 0 = 0000) = ( 0 = 0000) & ( 5 = 0101) | |
( 1 = 0001) = ( 1 = 0001) & ( 5 = 0101) | |
( 0 = 0000) = ( 2 = 0010) & ( 5 = 0101) | |
( 4 = 0100) = ( 4 = 0100) & ( 5 = 0101) | |
( 0 = 0000) = ( 8 = 1000) & ( 5 = 0101) | |
Bitwise Inclusive OR | |
( 5 = 0101) = ( 0 = 0000) | ( 5 = 0101) | |
( 5 = 0101) = ( 1 = 0001) | ( 5 = 0101) | |
( 7 = 0111) = ( 2 = 0010) | ( 5 = 0101) | |
( 5 = 0101) = ( 4 = 0100) | ( 5 = 0101) | |
(13 = 1101) = ( 8 = 1000) | ( 5 = 0101) | |
Bitwise Exclusive OR (XOR) | |
( 5 = 0101) = ( 0 = 0000) ^ ( 5 = 0101) | |
( 4 = 0100) = ( 1 = 0001) ^ ( 5 = 0101) | |
( 7 = 0111) = ( 2 = 0010) ^ ( 5 = 0101) | |
( 1 = 0001) = ( 4 = 0100) ^ ( 5 = 0101) | |
(13 = 1101) = ( 8 = 1000) ^ ( 5 = 0101) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment