return 0 != (m_flags & NODE_RENDERABLE)
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.
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
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
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
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).
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).
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
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
& 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.
&& 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.
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)
if (x) and if (x != false) are both much better than if (x == true)
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).
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement