什么是布尔标志

发布于 2024-12-07 10:42:54 字数 146 浏览 1 评论 0原文

我正在学习 Visual Basic 2010 课程,并试图掌握这个称为“标志”的新术语。我有点理解它与布尔条件有关。我不太明白flag是什么。我看到使用术语“标志”来引用它。我知道当布尔值、条件触发标志时,它会发生一些事情。但旗帜是什么。你如何识别它?有人可以给我举个例子吗?

I'm taking a course in Visual Basic 2010 and I'm trying to get a grasp on this new term called a flag. I kind of understand that it has something to do with a boolean condition. I don't quite understand what a flag is. I see references to it using the term flag. I understand it has something to do when a boolean, a condition triggers a flag. But what is the flag. How do you identify it? Can somebody give me an example.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

旧梦荧光笔 2024-12-14 10:42:55

一般来说,“标志”只是真/假条件的另一个术语。

在更具体的上下文中,它可能具有更具体的含义。例如,CPU 可以保留“算术标志”,每个标志都指示前一算术运算产生的真/假条件。例如,如果先前的操作是“ADD”,则标志将指示加法的结果是零、小于零还是大于零。

我相信这个术语来自用于表示通行/不通行条件的旗帜,例如铁路旗手指示火车是否安全行驶。

In general, "Flag" is just another term for a true/false condition.

It may have more specific meanings in more specific contexts. For instance, a CPU may keep "arithmetic flags", each one indicating a true/false condition resulting from the previous arithmetic operation. For instance, if the previous operation was an "ADD", then the flags would indicate whether the result of the add was zero, less than zero, or greater than zero.

I believe the term comes from flags used to signal a go/no go condition, like, a railroad flagman indicating whether or not it is safe for the train to proceed.

忆梦 2024-12-14 10:42:55

您经常听到 BOOL 是“标志”,因为只有 2 个结果,TRUE 或 FALSE。在决策过程中使用 BOOL 是一种在满足条件时“标记”特定结果的简单方法。

一个例子可以是:

if ($x == TRUE) {
 // DO THIS
{
else {
 //Flag not tripped, DO THIS
}

You hear this quite a bit with BOOL being a 'Flag' since there are only 2 outcomes either TRUE or FALSE. Using BOOL in your decision making processes is an easy way to 'flag' a certain outcome if the condition is met.

An example could be:

if ($x == TRUE) {
 // DO THIS
{
else {
 //Flag not tripped, DO THIS
}
铜锣湾横着走 2024-12-14 10:42:55

您可以将其与按位运算一起使用。它可用于将 32 个布尔值打包为一个整数。下面是一个示例:

Dim flags As Integer
Const ADMINISTRATOR = 1
Const USER = 2
Const BLUE = 4
Const RED = 8

flags = ADMINISTRATOR or BLUE

If flags and ADMINISTRATOR then
    ' Do something since the person is an admin
End If

or 添加标志,and 检查标志是否已设置。

现在我们可以为这一变量检查最多 32 个布尔值。非常适合存储在数据库中。您可以使用更大的数据类型(例如 long)来存储更多数据。

You can use this with bitwise operations. It can be used to pack 32 booleans into one integer. Here's a sample:

Dim flags As Integer
Const ADMINISTRATOR = 1
Const USER = 2
Const BLUE = 4
Const RED = 8

flags = ADMINISTRATOR or BLUE

If flags and ADMINISTRATOR then
    ' Do something since the person is an admin
End If

The ors add flags and ands check if the flag is set.

Now we can check up to 32 booleans for this one variable. Great for storing in a database. You can use bigger datatypes, like a long to store more.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文