Delphi中的按位标志 [英] Bitwise flags in Delphi

查看:99
本文介绍了Delphi中的按位标志的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I need to check if a certain flag is set for an integer.

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

I already know how to set a flag:

flags := FLAG_A or FLAG_B or FLAG_C

但是如何检查是否设置了某个标志?

But how can I check if a certain flag is set?

在C ++中,我使用了& 运算符,但是在Delphi中如何工作?我现在有点困惑

In C++ I used the & operator, but how does that work in Delphi? I'm a bit confused at the moment

推荐答案

在Delphi中,您有2种选择:

In Delphi you have 2 options:

1)使用'and'运算符,像这样:

1) use 'and' operator, like this:

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中设置)定义集合类型:

2) define set type:

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中的按位标志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆