Help with Bitmasks
Hi all!! Happy holidays!
I'm trying to get a grasp around this idea of bitmasks, and I'm kinda stumped. I think I've got a decent idea of what's going on but I can't get my little play script to react the way I expect it to! So I'm looking to you peoples for wise knowledge. Here's my test script:
$userBitmask = 1011;
define("CAN_LOG_IN", 1);
define("CAN_EDIT", 2);
define("CAN_DELETE", 4);
define("CAN_ADMIN", 8);
if (CAN_LOG_IN & $userBitmask) {
echo "According to the user bitmask of '$userBitmask', the user can log in.";
} else { echo "Nope. User cannot log in."; }
if (CAN_EDIT & $userBitmask) {
echo "According to the user bitmask of '$userBitmask', the user can edit.";
} else { echo "Nope. User cannot edit."; }
if (CAN_DELETE & $userBitmask) {
echo "According to the user bitmask of '$userBitmask', the user can delete.";
} else { echo "Nope. User cannot delete."; }
if (CAN_ADMIN & $userBitmask) {
echo "According to the user bitmask of '$userBitmask', the user can admin.";
} else { echo "Nope. User cannot administer."; }
// OUTPUT:
// ---------------------------------------- -------------------------
// According to the user bitmask of '1011', the user can log in.
// According to the user bitmask of '1011', the user can edit.
// Nope. User cannot delete.
// Nope. User cannot administer.
// ---------------------------------------- -------------------------
Any idea what I'm doing wrong here?? All help would be greatly appreciated.
I'm trying to get a grasp around this idea of bitmasks, and I'm kinda stumped. I think I've got a decent idea of what's going on but I can't get my little play script to react the way I expect it to! So I'm looking to you peoples for wise knowledge. Here's my test script:
$userBitmask = 1011;
define("CAN_LOG_IN", 1);
define("CAN_EDIT", 2);
define("CAN_DELETE", 4);
define("CAN_ADMIN", 8);
if (CAN_LOG_IN & $userBitmask) {
echo "According to the user bitmask of '$userBitmask', the user can log in.";
} else { echo "Nope. User cannot log in."; }
if (CAN_EDIT & $userBitmask) {
echo "According to the user bitmask of '$userBitmask', the user can edit.";
} else { echo "Nope. User cannot edit."; }
if (CAN_DELETE & $userBitmask) {
echo "According to the user bitmask of '$userBitmask', the user can delete.";
} else { echo "Nope. User cannot delete."; }
if (CAN_ADMIN & $userBitmask) {
echo "According to the user bitmask of '$userBitmask', the user can admin.";
} else { echo "Nope. User cannot administer."; }
// OUTPUT:
// ----------------------------------------
// According to the user bitmask of '1011', the user can log in.
// According to the user bitmask of '1011', the user can edit.
// Nope. User cannot delete.
// Nope. User cannot administer.
// ----------------------------------------
Any idea what I'm doing wrong here?? All help would be greatly appreciated.
