Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

groovy ^ operator

When given two boolean arguments, the ^ operator performs exclusive or, e.g.

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

When given two numeric arguments, it does something, but I've no idea what. At first I thought it was modular division because

(5 ^ 5) == 0

However

(10 ^ 4) == 14

So it's not modular division, is it some kind of bit-shifting?

like image 561
Dónal Avatar asked Aug 17 '11 15:08

Dónal


1 Answers

^ does the same thing as it does in Java and most other languages:

It's a bitwise exclusive OR (short: bitwise XOR)

This means that for every bit in the binary representation of the two numbers the resulting bit in the output will be the bit_in_first_value ^ bit_in_second_value.

like image 92
Joachim Sauer Avatar answered Sep 22 '22 22:09

Joachim Sauer