• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Doubt in exclusive OR operator

 
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
SCJP 6 K&B book -> Chapter 4: Operators -> page 309.

At one point it says that -
For an exclusive-OR (^) expression to be true, EXACTLY one operand must be true�for example.
System.out.printIn ("xor " + ((2<3) ^ (4>3)));

At another point it says that -
The preceding expression evaluates to false because BOTH operand one (2 < 3) and operand two (4 > 3) evaluate to true.

I am not able to digest the above things. May be I am confused with the exclusive OR(^) operator. Could someone help me up about this operator?
 
Ranch Hand
Posts: 210
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You will understand it better if you think of it as an OR operator, except that it will return false even when both the conditions are true.

returns false because both conditions evaluate to true i.e. 2 < 3 is true
and so is 4 > 3
 
Sheriff
Posts: 9709
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
exclusive or (AKA Xor) returns true if one of the operands is true.

If you go into some more depth, Xor will return true only if the number of true expressions is odd i.e. 1,3,5 etc.

eg

true ^ true ^ false

will return false...
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rajshekhar,

The exclusive OR operator works as follows:

true ^ true = false
true ^ false = true
false ^ true = true
false ^ false = false

when both the operands are of the same value whether it is true or false, the result is false. Otherwise the result is true.Hope this clarifies.


Thanks,
Maya
 
Rajshekhar Paul
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Prateek, Ankit and Maya for the helpful replies. I got it now.
[ December 10, 2008: Message edited by: Rajshekhar Paul ]
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic