整数标志如何工作? [英] How do Integer flags work?

查看:131
本文介绍了整数标志如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白标志是如何工作的,并希望有一些帮助和一个教程的链接来尝试了解将值添加到整数作为标志时发生的情况。

I do not understand how flags work and would appreciate some help and a link to a tutorial to try and understand what is happening when adding values to an Integer as flags.

例如,我将各种尺寸的文件备份到SD卡。许多文件通常已经存在,所以我首先运行并检查 FileExists FileAge 和大小,以及如果合适,将其添加到列表中以与 ShFileOperation 进行复制。使用Peter Barlow从这里开始的例行程序正在工作,但我想抑制某些事情。

For example, I am backing up various-sized files to an SD card. A lot of the files often already exist, so I first run through and check FileExists, FileAge, and size, and if appropriate, add it to the list to copy across with ShFileOperation. Using a routine by Peter Barlow from here it is working but I want to suppress some things.

如果文件大于10M,那么我想显示获胜进度bar,我不想提示覆盖现有的文件。另外,我不想让它检查是否有足够的空间,因为我已经做了这样的工作,所以我知道有足够的空间,所有的更新。

If the file is larger than 10M then I want to show the win-progress bar, I do not want a prompt for overwriting an existing file. Plus, I do not want it to check if there is enough room as I will have already done that so I know there is enough room for all the updates.

我有尝试使用 AND OR + - 但似乎无法找出我需要的组合。

I have tried messing with AND and OR and + and - but can't seem to figure out the combination I need.

if aBigFile then
  OpStruc.fFlags:=FOF_NOCONFIRMATION or FOF_SIMPLEPROGRESS else
  OpStruc.fFlags:=FOF_SILENT or FOF_NOCONFIRMATION;

有人可以给我一个我需要做的工作的例子吗?

Could someone please give me an example of what I need to make this work?

推荐答案

按位运算符。这意味着它们独立地应用于操作数的每一位。当学习按位操作时,它有助于以二进制而不是十进制绘制操作数。 (一旦你熟悉它们的工作原理,十进制仍然不是理想的格式;大多数人更喜欢十六进制,或者在较小的程度上,八进制。)

And and or are bitwise operators. That means they apply to each bit of their operands independently. When learning about bitwise operations, it helps to picture the operands in binary rather than decimal. (Once you're familiar with how they work, decimal still isn't the ideal format; most people prefer hexadecimal instead, or, to a lesser extent, octal.)

对于,当且仅当两个操作数的位被置位时,结果位被置位;否则结果被清除。对于,如果设置了操作数位的一个,则设置结果。当都设置 时,它也会被设置,因此它被称为包含或。 (还有独占或,其结果只有在设置了两个操作数中的一个时才设置,Delphi使用 xor 运算符。 )

For and, the result bit is set if and only if the bits of both operands are set; otherwise, the result is cleared. For or, the result is set if either one of the operand bits is set. It's also set when both are set, so it's known as inclusive or. (There's also exclusive or, where the result is set only when exactly one of the two operands is set. Delphi uses the xor operator for that.)

为了计算大于一位的操作数的结果,这些位是并行计算的,因此结果的第一位是从操作数,第二位的第二位等等。

To compute a result for operands that are larger than one bit, the bits are computed in parallel, so the first bit of the result is computed from the first bits of the operands, the second bit from the second bits, and so on.

组合标志时,使用以表示您希望 标志设置。例如:

When combining flags, you use or to indicate that you want both flags set. For example:

// Indicate we want both a simple progress bar *and* a lack of confirmation.
OpStruc.fFlags := fof_NoConfirmation or fof_SimpleProgress;

二进制的第一个标志的值是00010000.第二个是000100000000。这样做的结果是000100010000.

The first flag's value in binary is 00010000. The second's is 000100000000. That makes the result 000100010000.

设置标志时,您不会经常使用。相反,当您想要检查是否设置了标志时,通常会使用它。例如:

You won't frequently use and when setting flags. Rather, that's usually used when you want to check whether a flag is set. For example:

// Check whether the no-confirmation flag is set:
var FlagSet: Boolean;
FlagSet := (OpStruc.FFlags and fof_NoConfirmation) = fof_NoConfirmation;

由于输入为000100010000,因此标志值为000000010000.这等于标志值,因此 FlagSet 的值将为true。

Since the input was 000100010000, the result of the and with the flag value is 000000010000. That's equal to the flag value, so the value of FlagSet will be true.

有时足以检查操作的结果是否为零,而不是检查它是否完全等于你正在测试的旗帜当标志由多个设置位组成时,您希望检查它们的所有是否存在。如果只有一个存在,操作的结果仍然不为零。

It's sometimes enough to check whether the result of the and operation is non-zero, rather than to check that it's exactly equal to the flag you were testing. When the flag consists of multiple set bits, you want to check that all of them were present, though. If only one were present, the result of the and operation would still be non-zero.

To 删除标志,您使用反转要删除的标志位(清除所有设置位,反之亦然),然后使用来清除这些位。例如:

To remove flags, you use not to invert the bits of the flag you want to remove (clearing all the set bits and vice versa), and then use and to clear those bits. For example:

OpStruc.FFlags := OpStruc.FFlags and not fof_SimpleProgress;

fof_SimpleProgress 的倒数为111011111111。迄今为止,$ code> FFlags 的值为000100010000。将它们与组合将产生000000010000,这等于 fof_NoConfirmation ,正如我们预期的那样,删除 fof_SimpleProgress

The inverse of fof_SimpleProgress is 111011111111. The value of FFlags so far is 000100010000. Combining those with and will yield 000000010000, which equals fof_NoConfirmation, just as we'd expect by removing fof_SimpleProgress.

现在你了解各个标志值的组合方式,您可以指定要传递给您遇到的特定API函数的标志。阅读每个标志的文档,并确定它是否是您要使用的。如果是,请将其包含在您的标志列表中。

Now that you understand how individual flag values get combined, you're equipped indicate which flags you want to pass to the particular API function you're having trouble with. Read the documentation for each flag and decide whether it's one you want to use. If it is, include it in your list of flags.

可以以任何顺序将标志与组合。如果你知道你总是想要某些标志,而且你有条件地想要别人,你可以从你的公共标志列表开始,然后再添加。例如:

You can combine flags with or in any order. If you know you always want certain flags, and you conditionally want others, you can start with your list of common flags and then add others later. For example:

OpStruc.FFlags := fof_NoConfirmation or fof_SimpleProgress;
if BigFiles then
  OpStruct.FFlags := OpStruc.FFlags or fof_Silent;

它会频繁地使用加法来组合标志。显然, fof_NoConfirmation或fof_SimpleProgress 的结果等于 fof_NoConfirmation + fof_SimpleProgress 。然而,只有当每个标志的位值不同时,并且当不包含多个标志时,它才起作用。 fof_Silent或fof_Silent 的结果等于 fof_Silent ,而 fof_Silent + fof_Silent的结果 fof_RenameOnCollision ,这是一个完全不相关的标志。在使用标志时始终使用,从不 + -

It will frequently work to use addition to combine flags. Obviously, the result of fof_NoConfirmation or fof_SimpleProgress is equal to fof_NoConfirmation + fof_SimpleProgress. However, that only works when the bit values of each flag are distinct, and when no flag is included more than once. The result of fof_Silent or fof_Silent is equal to fof_Silent, whereas the result of fof_Silent + fof_Silent is fof_RenameOnCollision, which is an entirely unrelated flag. Always use and and or when working with flags, never + and -.

这篇关于整数标志如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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