gpt4 book ai didi

c++ - Bool 类型 True 和 False

转载 作者:太空宇宙 更新时间:2023-11-04 15:20:38 33 4
gpt4 key购买 nike

我在玩 Bool 类型( boolean 变量)并输入了这个:

#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;
}

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

bool $ok = false & true;

评估为假?

最佳答案

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

现在,当我们对 (false & true) 进行按位与运算时---(0&1=0)。

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

因此结果是 0(0x00000000)

关于c++ - Bool 类型 True 和 False,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20343114/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com