Jump to content
  • Advertisement
Sign in to follow this  
luasitdown

return 0 != (m_flags & NODE_RENDERABLE)

This topic is 4827 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

Advertisement
NODE_RENDERABLE is this context is most probably a #define thats set to some power of two. so, (m_flags & NODE_RENDERABLE) would be equal to NODE_RENDERABLE (provided that bit is set). However, when you say something like return (0==(m_flags & NODE_RENDERABLE)), it's compiler dependant as to what it returns exactly, you but can be sure it'll NOT be zero when (m_flags & NODE_RENDERABLE) returns 0.

Okay, my description doesn't make sense, so I'll draw it.

#define NODE_RENDERABLE 128
m_flags = 129;
// (m_flags & NODE_RENDERABLE) == 128
// (0==(m_flags & NODE_RENDERABLE) == 0
m_flags = 1;
// (m_flags & NODE_RENDERABLE) == 0
// (0==(m_flags & NODE_RENDERABLE) != 0

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
yes. it's exactly equal

if (m_flags & NODE_RENDERABLE) == true, you have 0 != 1 which is true


if (m_flags & NODE_RENDERABLE) == false, you have 0 != 0 which is false

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
No, it is not necessarily the same.

You are using a bitwise operator, which means that (m_flags & NODE_RENDERABLE) can be equal to any number in the range of integers.

so return (m_flags & NODE_RENDERABLE) can return any integer.


but 0 != (m_flags & NODE_RENDERABLE) is a conditional which will return only 0 or 1 (1 if integer is non-zero, 0 else).

Share this post


Link to post
Share on other sites
As the second AP posted, it's basically the same thing.

The 0 != stuff is there so that the compiler doesn't complain about converting an int to a bool. MSVC, at least, will give a warning for this.

j

Share this post


Link to post
Share on other sites
& is the bitwise operator
&& is the boolean operator

x & 128 yields 128 if the 128 bit is on.
x && 128 yields true if x is non-zero.

~ is a bitwise operator, ~0 is -1.
! is a boolean operator, so x!=128 would yield false when it makes sense.

Now, as for the specific values, the C specification says that 0 is false and false is 0. true is only specified as not false. So, true can be any integer value except zero. Careful, this DOES mean that true could be -1 (0xffffffff).

Last caviat, if works on true values. So, if (x==0) can be rewritten as if (!x). Its probably more readable that way actually, so go that way.

Share this post


Link to post
Share on other sites
Actually, the C++ standard states that true==1, I think, for backwards compatibility reasons, but it's best to always work as though true simply means "not 0".

if (x) and if (x != false) are both much better than if (x == true)

Share this post


Link to post
Share on other sites
Quote:
Original post by Anonymous Poster
No, it is not necessarily the same.

You are using a bitwise operator, which means that (m_flags & NODE_RENDERABLE) can be equal to any number in the range of integers.

so return (m_flags & NODE_RENDERABLE) can return any integer.


Not if you're in C++ and the return type is bool (which will result in the appropriate conversion as if the comparison to 0 were there).

Quote:
if (x) and if (x != false) are both much better than if (x == true)


Methinks that should be: "if (x) is generally much better than if (x != 0), which are both much better than if (x == true)" (assuming a well-named variable). The latter never adds useful information; normally it's better to just say "if (x)", but sometimes, going on to say "is nonzero" (i.e. "!= 0") can be clearer (this is up to the programmer's way of thinking and the team's coding standard).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • 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!