Jump to content
  • Advertisement

Archived

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

amish1234

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.

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
Advertisement
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 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. people like to post the EXACT SAME INFORMATION REPEATEDLY around here.

its annoying, but you get used to it.

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!