Skip to main content
GameDev.net gamedev.net
🔒 Locked

return 0 != (m_flags & NODE_RENDERABLE)

Started by luasitdown Aug 25, 2005 at 6:06 PM 6 replies 950+ views
Original Post
luasitdown
luasitdown
Is it exactly euqal return m_flags & NODE_RENDERABLE; why ?
Inmate2993
Inmate2993
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 128m_flags = 129;// (m_flags & NODE_RENDERABLE) == 128// (0==(m_flags & NODE_RENDERABLE) == 0m_flags = 1;// (m_flags & NODE_RENDERABLE) == 0// (0==(m_flags & NODE_RENDERABLE) != 0
william bubel
jdi
jdi
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
Inmate2993
Inmate2993
& 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.
william bubel
Squirm
Squirm
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)
Zahlman
Zahlman
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).

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.