Jump to content
  • Advertisement

Archived

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

Boops

Bitwise versus Logical operators

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

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
Advertisement
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

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!