Archived

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

Boops

Bitwise versus Logical operators

Recommended Posts

In C and similar languages, why is there made a difference between bitwise and logical operators? But both the logical and bitwise OR will only return 0 if both arguments are 0. Both logical and bitwise AND will return something that is not 0 if both arguments are non-zero (but I guess the difference is that the logical AND will return 1 while the bitwise returns some integer?). And XOR... There''s no logical XOR right? In a condition, could one use if(a & b) ...; instead of if(a && b) ...;?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
a && b is not the same as a & b
first a && b will return true or false(maybe TRUE or FALSE, in don''t know well)
while a & b only works for integers and will return and integer with the operation & on every bits....

hope it will help

Share this post


Link to post
Share on other sites
quote:
Original post by Anonymous Poster
while a & b only works for integers and will return and integer with the operation & on every bits....



Slight correction, it can be used with integral or enumeration operands, for example you can use it on a char.


Share this post


Link to post
Share on other sites
in binary,
00001111 &
10101010 =
00001010

00001111 |
10101010 =
10101111

00001111 ^
10101010 =
10100101

Bitwise operators are so called because they perform the operation on each individual bit. Logical operators perform it on the entire integer and will only return 0 to 1, never anything else.

~CGameProgrammer( );

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

Share this post


Link to post
Share on other sites
The logical operations have one behavior that bitwise ones don''t called "short-circuit evaluation" or some such thing. During runtime, if the expression''s outcome can be ascertained while evaluating one part of it, the rest can be ignored.

i.e.

if ( (0) && (1 / 0) ) {
/* do things */
}

This statement looks dangerous because the (1 / 0) will cause an arithmetic exception. However, the instruction pointer will never get there because it stops after the (0), knowing that it will force the expression to evaluate to 0 no matter what the second part says.

Likewise for

if ( (1) || (1 / 0) ) {
/* do things */
}

Try these with bitwise operations and see stuff crash.

-PJM

Share this post


Link to post
Share on other sites
quote:
Original post by pmargate
Try these with bitwise operations and see stuff crash.

-PJM


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.



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

[edited by - wild_pointer on November 15, 2003 8:11:34 PM]

Share this post


Link to post
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.[...]
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.

Share this post


Link to post
Share on other sites
quote:
Original post by wild_pointer
quote:
Original post by pmargate
Try these with bitwise operations and see stuff crash.

-PJM


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.



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

[edited by - wild_pointer on November 15, 2003 8:11:34 PM]



Yes, this would cause problems. Short circuit evaluation is a property of conditional expressions, of which bitwise operators are NOT. What you wrote would be akin to saying: if(0 + 1) is false because it sees the 0 first and thus short circuit evaluation comes into play and ignores the rest.

[edited by - YoshiN on November 15, 2003 10:28:33 PM]

Share this post


Link to post
Share on other sites
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
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