• Post Reply Bookmark Topic Watch Topic
  • New Topic

'^ exclusive or' usage  RSS feed

 
sai rama krishna
Ranch Hand
Posts: 536
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I do not see much of practical code using '^ exclusive or'

When do we use it. Please advise. Thanks in advance
 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, you use it when you really need XOR.

One example of its usage would be to calculate a hashCode of a Long.
 
Campbell Ritchie
Marshal
Posts: 56592
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is intended for integer numbers and it looks at the bit pattern. If both numbers have the same value of a particular bit, the new bit is set to 0. If they are different, to 1.
You can also use it for booleans because it has been overloaded. I can think of few instances where it is useful.That example from a Fraction class is ideal for the use of XOR.
 
sai rama krishna
Ranch Hand
Posts: 536
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(int)(value ^ (value >>> 32));

what it returns? value if it is greater than 32. why above code has > for three times. please advise
 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sai rama krishna wrote:(int)(value ^ (value >>> 32));

what it returns? value if it is greater than 32. why above code has > for three times. please advise


Operator >>> does not mean greater than. It is unsigned right shift operator.
You can read about what it does here.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!