This is giving me a headache...can someone please tell me why these statements ARE NOT equivalent?

Statement 1.)

if(running_result && shell[8 * (x - 1) + (y - 1)].input_val)
running_result = true;
else
running_result = false;


Statement 2.)

running_result &= shell[8 * (x - 1) + (y - 1)].input_val;

-------------------------
The first statement works; the second does NOT. The second always evaluates to false. Why? Both of these vars are bools.

This is giving me a headache...can someone please tell me why these statements ARE NOT equivalent?
-------------------------
The first statement works; the second does NOT. The second always evaluates to false. Why? Both of these vars are bools.

Maybe the problem is elsewhere.

#include <iostream>
  
  int main()
  {
     bool a, b;
  
     //Statement 1.)
     a = false; b = false; std::cout << "a = " << a << ", b = " << b;
     if ( a && b )
  	  a = true;
     else
  	  a = false;
     std::cout << ": a = " << a << std::endl;
  
     a = false; b = true; std::cout << "a = " << a << ", b = " << b;
     if ( a && b )
  	  a = true;
     else
  	  a = false;
     std::cout << ": a = " << a << std::endl;
  
     a = true; b = false; std::cout << "a = " << a << ", b = " << b;
     if ( a && b )
  	  a = true;
     else
  	  a = false;
     std::cout << ": a = " << a << std::endl;
  
     a = true; b = true; std::cout << "a = " << a << ", b = " << b;
     if ( a && b )
  	  a = true;
     else
  	  a = false;
     std::cout << ": a = " << a << std::endl;
     std::cout << std::endl;
  
     //Statement 2.)
     a = false; b = false; std::cout << "a = " << a << ", b = " << b;
     a &= b;
     std::cout << ": a = " << a << std::endl;
  
     a = false; b = true; std::cout << "a = " << a << ", b = " << b;
     a &= b;
     std::cout << ": a = " << a << std::endl;
  
     a = true; b = false; std::cout << "a = " << a << ", b = " << b;
     a &= b;
     std::cout << ": a = " << a << std::endl;
  
     a = true; b = true; std::cout << "a = " << a << ", b = " << b;
     a &= b;
     std::cout << ": a = " << a << std::endl;
     std::cout << std::endl;
  
     return 0;
  }
  
  /* my output
  a = 0, b = 0: a = 0
  a = 0, b = 1: a = 0
  a = 1, b = 0: a = 0
  a = 1, b = 1: a = 1
  
  a = 0, b = 0: a = 0
  a = 0, b = 1: a = 0
  a = 1, b = 0: a = 0
  a = 1, b = 1: a = 1
  */

the first statements uses logical and (&&) the second uses bitwise and (&), so you might try using '&&=' in the second statement.

The difference might be subtle; say the input_val was '2'. In the first statement the running result would be true and in the second statement it would be 0 because TRUE (1) & 2 == 0, but (1 && 2) is true.

the first statements uses logical and (&&) the second uses bitwise and (&), so you might try using '&&=' in the second statement.

There is no such critter: '&&='.

In any case, & and && are not the same.

But for bools, the results should be the same, shoudn't they?

Why? Both of these vars are bools.

I am so excited because I finished my program.

Chainsaw is right, any nonzero value is true in a bool, so a bitwise AND wouldn't necessarily evaluate to true... and I shouldn't have tried using a bitwise op for boolean algebra.

(Incidentally I settled on):

running_result = running_result && shell[8 * (x - 1) + (y - 1)].input_val;

Not too stylish, but it served its purpose.

This article has been dead for over six months. Start a new discussion instead.