Logical or and bitwise or

Started by
13 comments, last by peter86 21 years, 4 months ago
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 = 11 1 = 10 1 = 11 1 = 10 0 = 00 1 = 10 1 = 11 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.

Advertisement
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 birthif(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 blueint 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 placered   <<=  16;    // red   = 00000000 11110001 00000000 00000000green <<=  8;     // green = 00000000 00000000 11100011 00000000blue  <<=  0;     // blue  = 00000000 00000000 00000000 10001001 // Then we OR them to a 32-bit (actually 24-bit) RGB valueint 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  


I´m beginning to understand now, thanks guys.

[edited by - peter86 on December 4, 2002 2:17:25 PM]
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]
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
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!

This topic is closed to new replies.

Advertisement