How can you test which bits are set in a decimal number in PHP?
To test which bits are set in a decimal number in PHP, you can use the bitwise AND operator (&) with a bitmask that has only one bit set at a time. By performing bitwise AND operations between the decimal number and different bitmasks, you can determine which bits are set in the number.
$decimalNumber = 42; // Example decimal number
$bitmasks = [1, 2, 4, 8, 16, 32, 64, 128]; // Array of bitmasks with only one bit set at a time
foreach ($bitmasks as $bitmask) {
if ($decimalNumber & $bitmask) {
echo "Bit $bitmask is set in the decimal number.\n";
}
}