简体   繁体   English

布尔类型True和False

[英]Bool type True and False

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. 我知道使用按位运算符&和逻辑运算符&&之间的区别,但是我看不到它如何产生错误的(0)值。 I know if I swapped the bitwise operator and used a + the expression 0+1 would cause it to evaluate to true . 我知道如果我交换按位运算符并使用+则表达式0+1会使它的计算结果为true Can someone explain why this: 有人可以解释为什么:

bool $ok = false & true;

evaluates to false? 评估为假?

false=0(0x00000000) true=1(0x00000001) false = 0(0x00000000)true = 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)

Why would this be true? 为什么会这样呢? false converts to a 0-valued integer. false转换为0值的整数。 true converts to a non-zero valued integer (normally 1, but this is not guaranteed). true转换为非零值整数(通常为1,但这不能保证)。 0 & x for any x is always 0 . 任何x 0 & x始终为0 0 == false by definition of the integer/boolean interactions, thus the false branch is entered. 0 == false根据整数/布尔相互作用的定义为0 == false ,因此进入false分支。


For what it's worth, over a domain of 0 and 1, with 0 as false and 1 as true, * maps to AND whereas + maps to OR . 对于它的值,在0和1的域中,0表示false,1表示true, *映射为AND+映射为OR Given this, I'm not quite sure why you'd expect + and & to give the sameresults. 鉴于此,我不太确定为什么您期望+&给出相同的结果。

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. 如果要将整数视为位域,请使用无符号整数类型,其中围绕操作的规则更加自然和直观。

It's because false is 0 (when converted from boolean-land to integer-land), while true is 1 (when converted from boolean-land to integer-land). 这是因为false为0(从布尔值转换为整数值时),而true为1(从布尔值转换为整数值时)。

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 . 如果&的魔力对您来说是个谜,那么按位-和有很多很棒的资源

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为布尔类型定义名称,以代替“ true”和“ false” - Defining names in place of “true” and “false” for bool type NaN对Bool的转换:对还是错? - NaN to Bool conversion: True or False? 专注于布尔值的对与错 - Specializing both true and false for bool 有没有一种方法可以将模板的替换失败转换为bool值(true / false)或标签(std :: true_type / std :: false_type) - is there a way to convert a template's substitution failure into a bool value (true/false) or tags (std::true_type/std::false_type) 当我向布尔类型的向量分配true或false时,为什么下面的程序给我错误? - Why is below program giving me error when I am assigning true or false to a vector of type bool? 如何删除则返回true,否则返回false - How to return true if deleted and false if not with bool C++ bool 返回 0 1 而不是真假 - C++ bool returns 0 1 instead of true false OpenCV 如果图像是`==`,如何获得布尔真,否则如何获得假? - OpenCV how to get bool true if images are `==`and false if not? 在C ++中应该= false时Bool = true - Bool = true when it should = false in C++ 提升:如何将像“true”或“1”这样的字符串转换为bool true,将“0”或“false”转换为bool false? - Boost: how to convert string like “true” or “1” to bool true and “0” or “false” to bool false?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM