C++ 布尔类型 True 和 False

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20343114/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 23:04:19  来源:igfitidea点击:

Bool type True and False

c++booleanvisual-c++

提问by Jake2k13

I was playing around with the Bool type (Boolean Variable) and typed this:

我在玩 Bool 类型(布尔变量)并输入以下内容:

#include <iostream>

int main(void)
{
using std::cout;
using std::cin;
using std::endl;

bool $ok = false & true;

if($ok == true)
{
    cout << "The value is True." << endl;
}
else if($ok == false)
{
    cout << "The value is false." << endl;
}

cin.get();
cin.get();
return 0;
}

I know the differences between using the bitwise operator &and the logical operator &&, but I do not see how this produces a false (0) value. I know if I swapped the bitwise operator and used a +the expression 0+1would cause it to evaluate to true. Can someone explain why this:

我知道使用按位运算符&和逻辑运算符之间的区别&&,但我看不出这是如何产生假 (0) 值的。我知道如果我交换按位运算符并使用 a+表达式0+1会导致它计算为true. 有人可以解释为什么会这样:

bool $ok = false & true;

evaluates to false?

评估为假?

回答by Santosh Sahu

false=0(0x00000000) true=1(0x00000001)

假=0(0x00000000)真=1(0x00000001)

Now when we do bitwise and operator of (false & true)---(0&1=0).

现在,当我们执行 (false & true) ---(0&1=0) 的按位和运算符时。

            0x00000000
         &  0x00000001
          -------------
           0x00000000

Hence the result is 0(0x00000000)

因此结果是 0(0x00000000)

回答by Cornstalks

It's because falseis 0 (when converted from boolean-land to integer-land), while trueis 1 (when converted from boolean-land to integer-land).

这是因为false是 0(从 boolean-land 转换为 integer-land 时),而true1(从 boolean-land 转换为 integer-land 时)。

false & true == 0 & 1 == 0 == false
false + true == 0 + 1 == 1 == true

If the magic of &is a mystery to you, there are lots of great resources on bitwise-and.

如果 的魔法对您来说&是个谜,那么按位与 上有很多很棒的资源

回答by Corbin

Why would this be true? falseconverts to a 0-valued integer. trueconverts to a non-zero valued integer (normally 1, but this is not guaranteed). 0 & xfor any xis always 0. 0 == falseby definition of the integer/boolean interactions, thus the false branch is entered.

为什么这是真的?false转换为 0 值的整数。true转换为非零值整数(通常为 1,但不能保证)。0 & x因为任何x总是00 == false根据整数/布尔交互的定义,因此进入了错误分支。



For what it's worth, over a domain of 0 and 1, with 0 as false and 1 as true, *maps to ANDwhereas +maps to OR. Given this, I'm not quite sure why you'd expect +and &to give the sameresults.

对于它的价值,在 0 和 1 的域上,0 为假,1 为真,*映射到AND+映射到OR. 鉴于此,我不太确定为什么您会期望+&给出相同的结果。

x * y != 0 iff x != 0 and y != 0
x + y != 0 iff x != 0 or y != 0


It's also worth mentioning that bit-wise operations on signed types tend to be a bad idea. If you're going to treat integers as bitfields, use unsigned integral types where the rules around the operations are much more natural and intuitive.

还值得一提的是,对有符号类型进行按位操作往往是一个坏主意。如果您要将整数视为位域,请使用无符号整数类型,其中运算规则更加自然和直观。