C++ syntax question
I was looking at this code:
if(Flags & 2)
Flags is DWORD. To me, this seems to be saying if the bitwise and operation with Flags and 2 is 1, then the if statement''s condition is true. Why couldn''t the author have just said
if(Flags == 2)
?
___________________________________________________________
Where to find the intensity (Updated Mar 20, 2004)
Because it may have more than the 2nd bit set in which case the first expression will be true while the second will be false.
because flags == 2 is only one case. flags & 2 could return true for any number of numbers, including 6 which is binary 110, 23 which is binary 10111 etc.....
Not so. Flags & 2 would produce a "true" result for 3, 6, 10, 14 and many other numbers besides. Any number with the second last bit set will work, eg. (binary)11101 0.
To reword your statement, this is saying if the bitwise AND operation between Flags and 2 is non-zero then the if statement is true.
To add complexity, imagine saying "if(Flags & 13)". If Flags was equal to 1 then the if statement would equate to true as (1 & 13) is non-zero (as it is equal to 1). 1, 4, 5, 8, 9, 12 and 13 would all be regarded as true in this case, as would many other larger numbers.
If the programmer needed all the bit positions equivalent to 13 being set then they might say "if((Flags & 13) == 13)".
R
--------------------------------------------------------------------------
There is no point in flaming if you''ve merely poured fuel on your own head
To reword your statement, this is saying if the bitwise AND operation between Flags and 2 is non-zero then the if statement is true.
To add complexity, imagine saying "if(Flags & 13)". If Flags was equal to 1 then the if statement would equate to true as (1 & 13) is non-zero (as it is equal to 1). 1, 4, 5, 8, 9, 12 and 13 would all be regarded as true in this case, as would many other larger numbers.
If the programmer needed all the bit positions equivalent to 13 being set then they might say "if((Flags & 13) == 13)".
R
--------------------------------------------------------------------------
There is no point in flaming if you''ve merely poured fuel on your own head
what do you mean "not so ?". I said exactly what you did, I just said numbers that flags & 2 would return true include 6 and 23. I didn''t say they were the only numbers. Obviously there''s thousands of numbers with their penultimate bit set (10).
yeah. people like to post the EXACT SAME INFORMATION REPEATEDLY around here.
its annoying, but you get used to it.
its annoying, but you get used to it.
Often, the same answer gets posted multiple times, because neither poster saw the other reply. It takes time to compose a reply; meanwhile, someone else presses submit. End result: two similar responses.
yeah, like he said, often they don''t see the reply in time, they just respond at the same time
- brought to you by The Society for the Elimination, Eradication, and Extermination of all things Redundant, Repetitive, and Tautalogical.
- brought to you by The Society for the Elimination, Eradication, and Extermination of all things Redundant, Repetitive, and Tautalogical.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement