#### Archived

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

# Logical or and bitwise or

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

## Recommended Posts

What´s the diffrence between a logical or (||) and a bitwise or (|)?

##### Share on other sites
Logical or treats each operand as a boolean value, either true or false, and returns a boolean value.

Bitwise or treats each bit as a separate logical operation from each operand, and returns the result of all bits.

23 = 00010111 binary
199 = 11000111 binary

23 || 199 = (some nonzero [true] value)
23 | 199 = 00000111 binary = 7

Don''t listen to me. I''ve had too much coffee.

##### Share on other sites
A logical or produces a true or false value depending on the result of the operation. A bitwise or changes the bits of the result - basically merging the bits of the two operands into the result.

"Beautiful maiden," answered Candide, "when a man is in love, is jealous, and has been flogged by the Inquisition, he becomes lost to all reflection."

##### Share on other sites
quote:
Original post by Sneftel
Logical or treats each operand as a boolean value, either true or false, and returns a boolean value.

Bitwise or treats each bit as a separate logical operation from each operand, and returns the result of all bits.

23 = 00010111 binary
199 = 11000111 binary

23 || 199 = (some nonzero [true] value)
23 | 199 = 00000111 binary = 7

<hr>Don''t listen to me. I''ve had too much coffee.

Wouldn''t 23 | 199 =11010111 binary (no idea what that is)?

Or do I misunderstand bitwise operators?

##### Share on other sites
Sorry, that was me in the last post.

##### Share on other sites
quote:
Original post by Anonymous Poster (andromeda)
Wouldn''t 23 | 199 =11010111 binary (no idea what that is)?
Yes, Sneftel obviously did an bitwise AND instead of OR.

##### Share on other sites
quote:
A bitwise or changes the bits of the result - basically merging the bits of the two operands into the result.

And that means?
Could someone please give some examples and compare them with a logical or so I can see the diffrence?

##### Share on other sites
23 = 00010111 binary
199 = 11000111 binary

23 | 199 (bitwise OR - set a bit if it is set in the 1st num OR if it is set in the 2nd num [or if its set in both]) = 11010111 = 215

23 || 199 (logical OR - 23 is true OR 199 is true. Since 23 exists (is not 0) and 199 exists (is not 0), result: ) = true

I tend to think of logical ORs as "if a is not zero or b is not zero then...", because false is the same as zero, and true = not false, so true is any number except zero (including negatives).

Sorry if thats a little confusing.

##### Share on other sites
quote:
Original post by Evil Bill
23 | 199 (bitwise OR - set a bit if it is set in the 1st num OR if it is set in the 2nd num [or if its set in both]) = 11010111 = 215

How can it be 215? Shouldn´t it be just true or false (1 or 0)? And for what purposes is bitwise or used?

##### Share on other sites
quote:
Original post by peter86
And that means?

A logical operation implicitly converts both operands to bool, according to the conversion rules for doing that (e.g. non-zero int becomes true). The result is a bool. The Standard guarantees that a logical operator will have left-to-right evaluation order and that side effects of the first expression are executed before the second expression is evaluated.

A boolean operation does not perform any such conversions, and it does not provide any guarantees about evaluation order.

Example:

  #include <assert.h>int main(){	int x = 0x10;	int y = 0x20;	int logical_result = 0x10 || 0x20;	int bitwise_result = 0x10 | 0x20;	assert(logical_result == true);	assert(bitwise_result == 0x30);}

1. 1
2. 2
Rutin
15
3. 3
4. 4
5. 5

• 9
• 9
• 14
• 12
• 10
• ### Forum Statistics

• Total Topics
633269
• Total Posts
3011145
• ### Who's Online (See full list)

There are no registered users currently online

×