DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C++ (http://www.daniweb.com/forums/forum8.html)
-   -   Bitwise AND (http://www.daniweb.com/forums/thread10649.html)

iamhe Sep 11th, 2004 9:22 pm
Bitwise AND
 
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.

Dave Sinkula Sep 13th, 2004 1:06 pm
Re: Bitwise AND
 
Quote:

Originally Posted by iamhe
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
  */

Chainsaw Sep 13th, 2004 2:44 pm
Re: Bitwise AND
 
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.

Dave Sinkula Sep 13th, 2004 2:56 pm
Re: Bitwise AND
 
Quote:

Originally Posted by Chainsaw
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: '&&='.

Chainsaw Sep 13th, 2004 5:59 pm
Re: Bitwise AND
 
Hee hee - well there SHOULD be! In any case, & and && are not the same.

Dave Sinkula Sep 13th, 2004 7:05 pm
Re: Bitwise AND
 
Quote:

Originally Posted by Chainsaw
In any case, & and && are not the same.

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

Quote:

Originally Posted by iamhe
Why? Both of these vars are bools.


iamhe Sep 14th, 2004 6:51 am
Re: Bitwise AND
 
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.


All times are GMT -4. The time now is 9:02 pm.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC