Good morning....
Can someone please tell me the difference between
if (variable = 0)
and
if (variable == 0)
Thanks
Good morning....
Can someone please tell me the difference between
if (variable = 0)
and
if (variable == 0)
Thanks
One is a comparison, the other is an assignment.
I understand that but if I want to check for a value do I use the first or the second....I keep getting confused....
if (variable & 0x0F) .....Seems to me that here if ANY bit in the lower nibble of variable was set I would execute
if (variable == 0x0E) .....where as here the variable MUST be 0x0E to execute
Is this correct?
So the question begs 0 is 0 wouldn't both of the above statements cause execution if variable was 0?
Well if you use the assignment operator= in an if() statement the statement will always evaluate to true. If you use the comparison operator== it will evaluate to either true or false, depending on the values used in the comparison.
For the new post:
What is the type of variable?
For bit operations is is sometimes easier to use intermediate variables to check your logic.
And remember that in C false is zero and true is everything else (not zero).Code:value = variable & 0x0F; cout << value << '\n';
Also this link may be of some help when trying to figure out bitwise operations.
Here is an unsigned int returned from a function (within a structure)...I am trying to test to see if ANY one of the bits has been set not sure whether to use & or &&???
Code:(swInfo.twoSec & 0x00FE)
Just figured it out ...thank you!
used the debugger to see the difference
Last edited by ridgerunnersjw; 05-05-2020 at 12:25 PM.