Jake2k13 Jake2k13 - 25 days ago 9
C++ Question

Bool type True and False

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

#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+1
would cause it to evaluate to
true
. Can someone explain why this:

bool $ok = false & true;


evaluates to false?

Answer Source

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

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

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

Hence the result is 0(0x00000000)