• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

the Exclusive OR operator

 
Ranch Hand
Posts: 563
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I have a very basic question:
Exclusive or means that if the two operand bits are different the result is 1; otherwise the result is 0

I read in a book that 2^5 evaluates to 7. How is that possible ?
 
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


So, the result of an XOR would be

 
Celinio Fernandes
Ranch Hand
Posts: 563
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I still dont get it: shouldn't the result be 1 or 0 ?
Why are we adding the 2 operands here ?
[ September 22, 2005: Message edited by: Max longbeach ]
 
Ranch Hand
Posts: 214
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The operands 2 and 5 are not being added, although addition in this case produces the same result, which may be confusing you. The operands are the individual bits that represent each number in binary notation, not decimal notation.

Here's another example 2^3=1.

The ^ XOR operator works at the bit level, so convert the decimal numbers to binary:
2 = 0010
3 = 0011
^ = 0001
 
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are thinking of a single-bit, exclusive-or operation:

0 EXOR 0 is 0
0 EXOR 1 is 1
1 EXOR 0 is 1
1 EXOR 1 is 0

But the Java ^ operation on integral types is a bitwise operation.
The operation defined above is applied to all the bits of the ^ operands,
in parallel, as the previous poster demonstrated.

Also note than ^ is defined for booleans, and does want you wish: the
result is true iff the operand have different boolean values:

false ^ false is false
false ^ true is true
true ^ false is true
true ^ true is false

But then again, you get the same result with boolean's == operation.
 
rubbery bacon. rubbery tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic