delphi - Delphi 中的按位标志

标签 delphi

我需要检查是否为整数设置了某个标志。

我已经知道如何设置标志:

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/

相关文章:

delphi - 在Delphi XE中设置运行时的onclick事件

delphi - Delphi 可以告诉我引发异常的例程的名称吗?

delphi - 有没有更简单的方法在 Delphi 中重构它?

delphi - 如何获得 Firemonkey 中的默认系统颜色?

python - 检测反射 DLL 注入(inject)

Delphi:检测何时创建新表单

delphi - 如何将 IAutoComplete 与 TStringsAdapter 一起使用?

delphi - TMenuItem-Shortcuts 覆盖 Controls 中的 Shortcuts (TMemo)

delphi - 在 Delphi 中从 TList 开头删除大块项目的有效方法是什么

Delphi控件可以模仿Firefox的 "Add-ons|Extension list"吗?