#### Archived

This topic is now archived and is closed to further replies.

# C++ syntax question

This topic is 5231 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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)

##### Share on other sites
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.

##### Share on other sites
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.....

##### Share on other sites
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

##### Share on other sites
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).

##### Share on other sites
his "not so" was to the OP

##### Share on other sites
yeah. people like to post the EXACT SAME INFORMATION REPEATEDLY around here.

its annoying, but you get used to it.

##### Share on other sites
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.

##### Share on other sites
Redundancy is quite common on the GameDev forums.

Tee hee!

##### Share on other sites
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.

1. 1
Rutin
25
2. 2
3. 3
4. 4
JoeJ
18
5. 5

• 14
• 14
• 11
• 11
• 9
• ### Forum Statistics

• Total Topics
631757
• Total Posts
3002131
×