vb.net 什么是布尔标志
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7630404/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is a boolean flag
提问by swydell
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.
我正在学习 Visual Basic 2010 的课程,并试图掌握这个称为标志的新术语。我有点理解它与布尔条件有关。我不太明白什么是旗帜。我看到使用术语标志对它的引用。我知道当一个布尔值、一个条件触发一个标志时它有一些事情要做。但国旗是什么。你如何识别它?有人可以给我一个例子。
回答by John Saunders
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.
在更具体的上下文中,它可能具有更具体的含义。例如,CPU 可能会保留“算术标志”,每个标志都指示由前一算术运算产生的真/假条件。例如,如果先前的操作是“ADD”,则标志将指示添加的结果是零、小于零还是大于零。
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.
我相信该术语来自用于表示通行/不通行状态的标志,例如,铁路旗手指示火车行驶是否安全。
回答by xXPhenom22Xx
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.
当 BOOL 是“标志”时,您会听到很多这样的说法,因为只有 TRUE 或 FALSE 两种结果。在您的决策过程中使用 BOOL 是一种在满足条件时“标记”特定结果的简单方法。
An example could be:
一个例子可能是:
if ($x == TRUE) {
// DO THIS
{
else {
//Flag not tripped, DO THIS
}
回答by Matt Williamson
You can use this with bitwise operations. It can be used to pack 32 booleans into one integer. Here's a sample:
您可以将其用于按位运算。它可用于将 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
The or
s add flags and and
s check if the flag is set.
该or
■添加标志和and
,如果设置了标志支票。
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.
现在我们可以为这个变量检查多达 32 个布尔值。非常适合存储在数据库中。你可以使用更大的数据类型,比如 along
来存储更多。