• ### What is your GameDev Story?

#### Archived

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

# Bitwise versus Logical operators

This topic is 5540 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## 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 on other sites
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 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 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 on other sites
quote:
Original post by Narcist
(1 && 2) == true
(1 & 2) == 0 == false

Good point.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 15
• 14
• 46
• 22
• 27
• ### Forum Statistics

• Total Topics
634047
• Total Posts
3015229
×