#### Archived

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

# Bitwise versus Logical operators

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

## 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 on other sites
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 on other sites

[edited by - Boops on November 15, 2003 2:50:03 PM]

##### 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 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 on other sites
(1 && 2) == true
(1 & 2) == 0 == false

##### 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 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 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 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]

1. 1
Rutin
41
2. 2
3. 3
4. 4
5. 5

• 16
• 18
• 12
• 14
• 9
• ### Forum Statistics

• Total Topics
633362
• Total Posts
3011527
• ### Who's Online (See full list)

There are no registered users currently online

×