CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

whats the difference between logical or and bitwise or

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
hadeelqasaimeh



Joined: 05 Jan 2006
Posts: 105

View user's profile Send private message

whats the difference between logical or and bitwise or
PostPosted: Tue Jun 13, 2006 12:15 pm     Reply with quote

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?
Code:
^

and this
Code:
^=

thanx alot
rberek



Joined: 10 Jan 2005
Posts: 207
Location: Ottawa, Canada

View user's profile Send private message

PostPosted: Tue Jun 13, 2006 12:32 pm     Reply with quote

^ is the XOR (exclusive OR) operator

^= is used thusly:

Code:
  x =^y


Which is exactly the same as:

Code:
  x = x^y
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

PostPosted: Tue Jun 13, 2006 12:45 pm     Reply with quote

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

View user's profile Send private message Visit poster's website

PostPosted: Tue Jun 13, 2006 2:10 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Jun 14, 2006 1:32 am     Reply with quote

ok,i got it.

so
Code:

(0x55^0x58)


will return the value of 0x55 exclusive or0x58.right?

thanks alot. Smile
treitmey



Joined: 23 Jan 2004
Posts: 1094
Location: Appleton,WI USA

View user's profile Send private message Visit poster's website

PostPosted: Wed Jun 14, 2006 7:54 am     Reply with quote

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

View user's profile Send private message Send e-mail

PostPosted: Wed Jun 14, 2006 12:02 pm     Reply with quote

which is 13 Smile
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group