![]() |
| ||
| 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. |
| ||
| Re: Bitwise AND Quote:
#include <iostream> |
| ||
| 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. |
| ||
| Re: Bitwise AND Quote:
|
| ||
| Re: Bitwise AND Hee hee - well there SHOULD be! In any case, & and && are not the same. |
| ||
| Re: Bitwise AND Quote:
Quote:
|
| ||
| 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