Bitwise AND

Reply

Join Date: Sep 2004
Posts: 2
Reputation: iamhe is an unknown quantity at this point 
Solved Threads: 0
iamhe iamhe is offline Offline
Newbie Poster

Bitwise AND

 
2
  #1
Sep 11th, 2004
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,314
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 229
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Bitwise AND

 
2
  #2
Sep 13th, 2004
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.
  1. #include <iostream>
  2.  
  3. int main()
  4. {
  5. bool a, b;
  6.  
  7. //Statement 1.)
  8. a = false; b = false; std::cout << "a = " << a << ", b = " << b;
  9. if ( a && b )
  10. a = true;
  11. else
  12. a = false;
  13. std::cout << ": a = " << a << std::endl;
  14.  
  15. a = false; b = true; std::cout << "a = " << a << ", b = " << b;
  16. if ( a && b )
  17. a = true;
  18. else
  19. a = false;
  20. std::cout << ": a = " << a << std::endl;
  21.  
  22. a = true; b = false; std::cout << "a = " << a << ", b = " << b;
  23. if ( a && b )
  24. a = true;
  25. else
  26. a = false;
  27. std::cout << ": a = " << a << std::endl;
  28.  
  29. a = true; b = true; std::cout << "a = " << a << ", b = " << b;
  30. if ( a && b )
  31. a = true;
  32. else
  33. a = false;
  34. std::cout << ": a = " << a << std::endl;
  35. std::cout << std::endl;
  36.  
  37. //Statement 2.)
  38. a = false; b = false; std::cout << "a = " << a << ", b = " << b;
  39. a &= b;
  40. std::cout << ": a = " << a << std::endl;
  41.  
  42. a = false; b = true; std::cout << "a = " << a << ", b = " << b;
  43. a &= b;
  44. std::cout << ": a = " << a << std::endl;
  45.  
  46. a = true; b = false; std::cout << "a = " << a << ", b = " << b;
  47. a &= b;
  48. std::cout << ": a = " << a << std::endl;
  49.  
  50. a = true; b = true; std::cout << "a = " << a << ", b = " << b;
  51. a &= b;
  52. std::cout << ": a = " << a << std::endl;
  53. std::cout << std::endl;
  54.  
  55. return 0;
  56. }
  57.  
  58. /* my output
  59.   a = 0, b = 0: a = 0
  60.   a = 0, b = 1: a = 0
  61.   a = 1, b = 0: a = 0
  62.   a = 1, b = 1: a = 1
  63.  
  64.   a = 0, b = 0: a = 0
  65.   a = 0, b = 1: a = 0
  66.   a = 1, b = 0: a = 0
  67.   a = 1, b = 1: a = 1
  68.   */
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 436
Reputation: Chainsaw is an unknown quantity at this point 
Solved Threads: 10
Chainsaw's Avatar
Chainsaw Chainsaw is offline Offline
Unprevaricator

Re: Bitwise AND

 
2
  #3
Sep 13th, 2004
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,314
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 229
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Bitwise AND

 
0
  #4
Sep 13th, 2004
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: '&&='.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 436
Reputation: Chainsaw is an unknown quantity at this point 
Solved Threads: 10
Chainsaw's Avatar
Chainsaw Chainsaw is offline Offline
Unprevaricator

Re: Bitwise AND

 
0
  #5
Sep 13th, 2004
Hee hee - well there SHOULD be! In any case, & and && are not the same.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,314
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 229
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Bitwise AND

 
0
  #6
Sep 13th, 2004
Originally Posted by Chainsaw
In any case, & and && are not the same.
But for bools, the results should be the same, shoudn't they?

Originally Posted by iamhe
Why? Both of these vars are bools.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 2
Reputation: iamhe is an unknown quantity at this point 
Solved Threads: 0
iamhe iamhe is offline Offline
Newbie Poster

Re: Bitwise AND

 
0
  #7
Sep 14th, 2004
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC