检查包含的十进制数的位标志
Check bit flags for included decimal number
我需要检查某个十进制变量 $decimal = 3660
包含在哪些标志中。我用十六进制常量设置了标志。之后函数“show_flags”应该输出包含十进制变量“3660”的每个标志。
我对位标志没有太多经验并尝试过,但我没有得到任何输出。这是 demo.
代码
$decimal= 3660;
const FLAG_1 = 0x1;
const FLAG_2 = 0x2;
const FLAG_3 = 0x4;
const FLAG_4 = 0x8;
const FLAG_5 = 0x10;
const FLAG_6 = 0x20;
const FLAG_7 = 0x40;
const FLAG_8 = 0x80;
const FLAG_9 = 0x100;
const FLAG_10 = 0x200;
const FLAG_11 = 0x400;
const FLAG_12 = 0x800;
function show_flags ($decimal) {
if ($decimal & FLAG_1) {
echo "Flag 1 included.<br>\n";
}
if ($decimal & FLAG_2) {
echo "Flag 2 included.<br>\n";
}
if ($decimal & FLAG_3) {
echo "Flag 3 included.<br>\n";
}
if ($decimal & FLAG_4) {
echo "Flag 4 included.<br>\n";
}
if ($decimal & FLAG_5) {
echo "Flag 5 included.<br>\n";
}
if ($decimal & FLAG_6) {
echo "Flag 6 included.<br>\n";
}
if ($decimal & FLAG_7) {
echo "Flag 7 included.<br>\n";
}
if ($decimal & FLAG_8) {
echo "Flag 8 included.<br>\n";
}
if ($decimal & FLAG_9) {
echo "Flag 9 included.<br>\n";
}
if ($decimal & FLAG_10) {
echo "Flag 10 included.<br>\n";
}
if ($decimal & FLAG_11) {
echo "Flag 11 included.<br>\n";
}
if ($decimal & FLAG_12) {
echo "Flag 12 included.<br>\n";
}
}
未使用该功能,这就是您看不到任何结果的原因。
用
调用它
show_flags($decimal);
显示结果符合预期。
Flag 3 included.<br>
Flag 4 included.<br>
Flag 7 included.<br>
Flag 10 included.<br>
Flag 11 included.<br>
Flag 12 included.<br>
我需要检查某个十进制变量 $decimal = 3660
包含在哪些标志中。我用十六进制常量设置了标志。之后函数“show_flags”应该输出包含十进制变量“3660”的每个标志。
我对位标志没有太多经验并尝试过,但我没有得到任何输出。这是 demo.
代码
$decimal= 3660;
const FLAG_1 = 0x1;
const FLAG_2 = 0x2;
const FLAG_3 = 0x4;
const FLAG_4 = 0x8;
const FLAG_5 = 0x10;
const FLAG_6 = 0x20;
const FLAG_7 = 0x40;
const FLAG_8 = 0x80;
const FLAG_9 = 0x100;
const FLAG_10 = 0x200;
const FLAG_11 = 0x400;
const FLAG_12 = 0x800;
function show_flags ($decimal) {
if ($decimal & FLAG_1) {
echo "Flag 1 included.<br>\n";
}
if ($decimal & FLAG_2) {
echo "Flag 2 included.<br>\n";
}
if ($decimal & FLAG_3) {
echo "Flag 3 included.<br>\n";
}
if ($decimal & FLAG_4) {
echo "Flag 4 included.<br>\n";
}
if ($decimal & FLAG_5) {
echo "Flag 5 included.<br>\n";
}
if ($decimal & FLAG_6) {
echo "Flag 6 included.<br>\n";
}
if ($decimal & FLAG_7) {
echo "Flag 7 included.<br>\n";
}
if ($decimal & FLAG_8) {
echo "Flag 8 included.<br>\n";
}
if ($decimal & FLAG_9) {
echo "Flag 9 included.<br>\n";
}
if ($decimal & FLAG_10) {
echo "Flag 10 included.<br>\n";
}
if ($decimal & FLAG_11) {
echo "Flag 11 included.<br>\n";
}
if ($decimal & FLAG_12) {
echo "Flag 12 included.<br>\n";
}
}
未使用该功能,这就是您看不到任何结果的原因。
用
调用它show_flags($decimal);
显示结果符合预期。
Flag 3 included.<br>
Flag 4 included.<br>
Flag 7 included.<br>
Flag 10 included.<br>
Flag 11 included.<br>
Flag 12 included.<br>