我需要检查是否为整数设置了某个标志。
我已经知道如何设置标志:
flags := FLAG_A or FLAG_B or FLAG_C
但是我如何检查是否设置了某个标志?
在 C++ 中我使用了 &
运算符,但是在 Delphi 中它是如何工作的呢?我现在有点困惑
最佳答案
在 Delphi 中你有 2 个选择:
1)使用“and”运算符,如下所示:
const
FLAG_A = 1; // 1 shl 0
FLAG_B = 2; // 1 shl 1
FLAG_C = 4; // 1 shl 2
var
Flags: Integer;
[..]
Flags:= FLAG_A or FLAG_C;
if FLAG_A and Flags <> 0 then .. // check FLAG_A is set in flags variable
2)定义集合类型:
type
TFlag = (FLAG_A, FLAG_B, FLAG_C);
TFlags = set of TFlag;
var
Flags: TFlags;
[..]
Flags:= [FLAG_A, FLAG_C];
if FLAG_A in Flags then .. // check FLAG_A is set in flags variable
关于delphi - Delphi 中的按位标志,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3728018/