Archived

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

C++ syntax question

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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.

Share this post


Link to post
Share on other sites
quote:
Original post by Zahlman
Redundancy is quite common on the GameDev forums.

Tee hee!


hehe, not only in replies:D (just saw yet another "my app uses 100% cpu power" post:D)




If that''s not the help you''re after then you''re going to have to explain the problem better than what you have. - joanusdmentia

davepermen.net

Share this post


Link to post
Share on other sites
It seems I caused a little consternation here. My "Not so" was not aimed at gowron67. It''s just that it took me a few minutes to write my reply and by then gowron67 had replied with a quick 2-liner.

Xai and hplus0603 picked it up. Thanks.

I guess it just helps to mention names more directly when replying to something. It''s also why using the quote feature is a good idea.

Next time...

R

--------------------------------------------------------------------------
There is no point in flaming if you''ve merely poured fuel on your own head

Share this post


Link to post
Share on other sites