• Advertisement

Archived

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

Logical or and bitwise or

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

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


Link to post
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 this post


Link to post
Share on other sites
Guest Anonymous Poster
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 this post


Link to 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 this post


Link to post
Share on other sites
quote:
Original post by LessBread
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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);
}

Share this post


Link to post
Share on other sites
quote:
Original post by peter86
Could someone please give some examples and compare them with a logical or so I can see the diffrence?
Here you have the truth table for OR:

0 0 = 0 (false)
0 1 = 1 (true)
1 0 = 1 (true)
1 1 = 1 (true)

If you have one bit, it is either 0 (false) or 1 (true). When you OR two values, you apply that truth table on them to calculate the result, which is either 0 (true) or 1 (false).

When doing a logical OR operation (i | j), both value are evaluated to either true (if non-zero) or false (if zero), and the operation is applied to those. That is, if both values are zero, the result is false, and in all other cases true.

When you do a bitwise OR operation (i || j), you perform that calculatation on each bit of the two values at the same position, and store the result in a third value at the same position. Here we do a bitwise or on two 8-bit values:

11010001 (= 209)
OR 01110110 (= 118)
===========--------
11110111 (= 247)

or for easier comparison with the table above:

1 0 = 1
1 1 = 1
0 1 = 1
1 1 = 1
0 0 = 0
0 1 = 1
0 1 = 1
1 0 = 1
I hope we''re getting somewhere. Otherwise, you should go out and find a book (or, failing that, a web page)about binary logic.


Share this post


Link to post
Share on other sites
Oh, and I try to give some example uses for them:

Logical OR

Handy when you want to succeed if either of several conditions are true
  
int user_dateofbirth;
int user_age;

// TODO: Request user input


// The user must supply either his/her age or date of birth

if(user_dateofbirth || user_age)
cout << "good boy!";
else
cout << "invalid input";

Bitwise OR

One of more common uses of bitwise OR today might be to construct color values
  
// Define color values for red, green and blue

int red = 0xf1, // red = 00000000 00000000 00000000 11110001

green = 0xe3, // green = 00000000 00000000 00000000 11100011

blue = 0x85; // blue = 00000000 00000000 00000000 10001001


// Now we shift some to get the ones to the right place

red <<= 16; // red = 00000000 11110001 00000000 00000000

green <<= 8; // green = 00000000 00000000 11100011 00000000

blue <<= 0; // blue = 00000000 00000000 00000000 10001001


// Then we OR them to a 32-bit (actually 24-bit) RGB value

int color = (red | green | blue);

// This operation ORed three values, but it actually did ((red | green) | blue)

// red = 00000000 11110001 00000000 00000000

// green = 00000000 00000000 11100011 00000000

// OR blue = 00000000 00000000 00000000 10001001

// ----------------------------------------------------

// color = 00000000 11110001 11100011 10001001




Share this post


Link to post
Share on other sites
I´m beginning to understand now, thanks guys.

[edited by - peter86 on December 4, 2002 2:17:25 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by LessBread
A bitwise or changes the bits of the result - basically merging the bits of the two operands into the result.


quote:
Original post by peter86
And that means?



Sorry for the vagueness. I think CWizards example with the colors provides an excellent illustration of bit merging.



"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."


[edited by - lessbread on December 4, 2002 4:35:19 PM]

Share this post


Link to post
Share on other sites
May as well learn a few other bitwise operations:

5 = 0101
14 = 1110

| (bitwise or) looks at each bit of the 2 numbers and compares them. If either is 1, then the corresponding bit in the result is also a 1.

& (bitwise and) looks at each bit, if BOTH bits are 1 then the corresponding result bit is 1, in any other case it is 0.

xor (exclusive bitwise or): If either of the 2 bits is 1, the corresponding result bit is 1. However, if BOTH bits are 1 or BOTH bits are 0 then the corresponding result bit is 0.

examples:

5 | 14:
0101 |
1110
result: 1111 = 15

5 & 14:
0101 &
1110
result: 0100 = 4

5 xor 14:
0101 &
1110
result: 1011 = 11

http://roninmagus.hopto.org
acronymfinder.com - Find any acronym you need!

Share this post


Link to post
Share on other sites

  • Advertisement