View previous topic :: View next topic |
Author |
Message |
hadeelqasaimeh
Joined: 05 Jan 2006 Posts: 105
|
whats the difference between logical or and bitwise or |
Posted: Tue Jun 13, 2006 12:15 pm |
|
|
hi all
whats the difference between logical or and bitwise or?really i need to make exclusive or through my code,what should i do?i saw an operator called bitwise exclusuive or.is this what i need?
and this
thanx alot |
|
|
rberek
Joined: 10 Jan 2005 Posts: 207 Location: Ottawa, Canada
|
|
Posted: Tue Jun 13, 2006 12:32 pm |
|
|
^ is the XOR (exclusive OR) operator
^= is used thusly:
Which is exactly the same as:
|
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Tue Jun 13, 2006 12:45 pm |
|
|
The logical OR uses the || operator, and the bitwise OR uses the | operator. A use of the logical OR might look something like this:
Code: | if ((x == 5) || (y == 7))
DoSomething();
|
In this example, the function will be called if x is 5, if y is 7, or both. The only way the function is not called is if both of the conditions are false. The bitwise OR is very similar, in that it returns 0 if and only if both of its operands are 0. To illustrate this, we have the following truth table:
0 OR 0 = 0
0 OR 1 = 1 x OR 0 = x
1 OR 0 = 1 x OR 1 = 1
1 OR 1 = 1
Let's look at an example of using the bitwise OR operation on two words:
Code: |
0110 1011 1000 0101
| 0001 1111 1011 1001
---------------------
0111 1111 1011 1101
|
Here you can see that the result contains a 0 only when the corresponding bits in both of the operands are also 0 and a 1 if either bit is a 1. |
|
|
treitmey
Joined: 23 Jan 2004 Posts: 1094 Location: Appleton,WI USA
|
|
Posted: Tue Jun 13, 2006 2:10 pm |
|
|
Logical OR || returns a int1(boolean) result.
if(x==5) || y==12) will return true or false.
the bit wise takes each bit of a number/variable and OR's it
0000 0111 y=0x07 | 0x05;
0000 0101
------------
0000 0111
can also use AND for a mask
0000 1111 y=0x0F & 0x05;
0000 0101
------------
0000 0101 I masked off the upper nibble
did that answer your question? or are you looking for a XOR operator. |
|
|
hadeelqasaimeh
Joined: 05 Jan 2006 Posts: 105
|
|
Posted: Wed Jun 14, 2006 1:32 am |
|
|
ok,i got it.
so
will return the value of 0x55 exclusive or0x58.right?
thanks alot. |
|
|
treitmey
Joined: 23 Jan 2004 Posts: 1094 Location: Appleton,WI USA
|
|
Posted: Wed Jun 14, 2006 7:54 am |
|
|
yes
Quote: | ^=
Bitwise exclusive or assignment operator, x^=y, is the same as x=x^y
^
Bitwise exclusive or operator
|
|
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Wed Jun 14, 2006 12:02 pm |
|
|
which is 13 |
|
|
|