Created
May 4, 2020 14:53
-
-
Save stevenrombauts/f51b8d93e14071d6a0fc0ae680bb52c1 to your computer and use it in GitHub Desktop.
Comparing bits
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 | |
// Always use a factor of 2 | |
$ADMIN = 1; | |
$TEACHER = 2; | |
$HUMAN = 4; | |
// This is how these numbers look in bits: | |
echo "Admin: " .decbin($ADMIN) . PHP_EOL; | |
echo "Teacher: ". decbin($TEACHER) . PHP_EOL; | |
echo "Human: ". decbin($HUMAN) . PHP_EOL; | |
// Let's combine two types: | |
$combined = $ADMIN | $TEACHER; | |
echo "Admin AND Teacher: " . decbin($combined) . PHP_EOL; | |
// Let's see what matches | |
if ($combined & $TEACHER) { | |
echo "is Teacher\n"; | |
} else { | |
echo "is not Teacher\n"; | |
} | |
if ($combined & $HUMAN) { | |
echo "is Human\n"; | |
} else { | |
echo " is not Human\n"; | |
} | |
if ($combined & ($TEACHER | $HUMAN)) { | |
echo "is Teacher OR Human\n"; | |
} else { | |
echo " is not Teacher OR Human\n"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment