Jump to content
  • Advertisement

Archived

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

Boops

Bitwise versus Logical operators

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

quote:
Original post by Extrarius
The latter one should cause problems, because ORing with the number one can result in any odd number. IF it shortcircuts all conditionals, then you would have to use the identity numbers for the operator for it to do so. Thus, you would have to OR with a number that has all bits set for any short circuiting to occur.


Yes, my bad. I was thinking of it as a single bit, which of course it's not.




[My site|SGI STL|Bjarne FAQ|C++ FAQ Lite|MSDN|Jargon]
Ripped off from various people

[edited by - wild_pointer on November 16, 2003 1:02:55 AM]

Share this post


Link to post
Share on other sites
Advertisement
quote:
Original post by wild_pointer
if( 0 & (1/0) ) would not result in a dbz, nor would if( 1 | (1/0) ). Short-circuit evaluation is a property of c++ conditional expressions, not what kind of operators you use.



Like YoshiN said, both of these will cause problems. Bitwise operators are evaluated no matter what. The only situation where this might work (on some compilers) is if you had the literal constant 0 as either operand of a bitwise and operation. Some compilers will be able to tell that it will always evaluate to zero, and will optimize it for you, removing the bitwise operation. If you had a variable however, it has to evaluate it.

Share this post


Link to post
Share on other sites
What you guys are referring to is the fact that logical operators govern program flow, whereas bitwise operators are merely arithmetic operations. It''s a very big difference. In this code:

if( Pointer != NULL && Pointer->GoodToGo )
{
}

That works fine because Pointer isn''t dereferenced if the first part of the conditional statement (Pointer != NULL) fails. But that has nothing to do with the instruction pointer -- it has to do with the compiler. It does, in assemble, the equivalent of this C++:

if( Pointer != NULL )
{
if( Pointer->GoodToGo )
{
}
}


~CGameProgrammer( );

-- Post screenshots of your projects. 100+ posts already in the archives.

Share this post


Link to post
Share on other sites
quote:
Original post by DukeAtreides076

Like YoshiN said, both of these will cause problems. Bitwise operators are evaluated no matter what. The only situation where this might work (on some compilers) is if you had the literal constant 0 as either operand of a bitwise and operation. Some compilers will be able to tell that it will always evaluate to zero, and will optimize it for you, removing the bitwise operation. If you had a variable however, it has to evaluate it.


The first one is fine just about anywhere. I was just trying to illustrate that the only difference is not that one allows short-circuiting and the other does not, which pmargate''s post implied (wether or not it''s what he meant). They''re quite different things, which has already been amply demonstrated.

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!