944,192 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2745
  • C++ RSS
Sep 11th, 2004
2

Bitwise AND

Expand Post »
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.
Similar Threads
Reputation Points: 12
Solved Threads: 0
Newbie Poster
iamhe is offline Offline
2 posts
since Sep 2004
Sep 13th, 2004
2

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.
C++ Syntax (Toggle Plain Text)
  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.   */
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Sep 13th, 2004
2

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.
Reputation Points: 36
Solved Threads: 11
Posting Pro in Training
Chainsaw is offline Offline
436 posts
since Jun 2004
Sep 13th, 2004
0

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: '&&='.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Sep 13th, 2004
0

Re: Bitwise AND

Hee hee - well there SHOULD be! In any case, & and && are not the same.
Reputation Points: 36
Solved Threads: 11
Posting Pro in Training
Chainsaw is offline Offline
436 posts
since Jun 2004
Sep 13th, 2004
0

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.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Sep 14th, 2004
0

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.
Reputation Points: 12
Solved Threads: 0
Newbie Poster
iamhe is offline Offline
2 posts
since Sep 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Refresher information for C++
Next Thread in C++ Forum Timeline: why i have to press "enter" twice before getline can read the string...





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC