0

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?

5
  • 2
    Why did you expect it to be true? Dec 3, 2013 at 4:51
  • I could be way off here, but wouldn't using the bitwise operator '&' with both the false and true predefined literals (0 ,1) that it would produce a true value? I don't know how/why it would or wouldn't, that's why I'm asking for help.
    – Jake2k13
    Dec 3, 2013 at 4:55
  • 1
    Do you know how to compute a bitwise and? If not, here's a reference. Short answer, no. Dec 3, 2013 at 4:57
  • Thanks, I don't know too much about it, I just know the differences between '&&' and '&' is all. I'm self taught and could use as many resources to learn as possible.
    – Jake2k13
    Dec 3, 2013 at 4:59
  • 1
    BTW do not use $ in variable names in C++ if you want the code to be portable. stackoverflow.com/questions/7926394/in-variable-name
    – Melebius
    Sep 22, 2017 at 5:36

3 Answers 3

5

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)

1

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


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. 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.

1

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 & 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.