Skip to content

Instantly share code, notes, and snippets.

@stevenrombauts
Created May 4, 2020 14:53
Show Gist options
  • Save stevenrombauts/f51b8d93e14071d6a0fc0ae680bb52c1 to your computer and use it in GitHub Desktop.
Save stevenrombauts/f51b8d93e14071d6a0fc0ae680bb52c1 to your computer and use it in GitHub Desktop.
Comparing bits
<?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