Groovy operator ^

When two logical arguments are given, the ^ operator performs exceptional or, for example,

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

When specifying two numerical arguments, he does something, but I have no idea what. At first I thought it was modular division, because

(5 ^ 5) == 0

However

(10 ^ 4) == 14

So this is not modular division, is it some kind of bit shift?

+5
source share
1 answer

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

This is a bitwise exclusive OR (short: bitwise XOR)

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

+12
source

All Articles