How to XOR two doubles in JAVA?
simple '^' doesn't work for doubles... Would I have to convert a double to binary form and do it bitwise? or is there any other way?
Java XOR Operator (Exclusive OR)It takes two values and returns true if they are different; otherwise returns false. In binary, the true is represented by 1 and false is represented by 0. From the above table, we can see it returns true if and only if both operand's values are different. Otherwise, it returns false.
XOR is defined as exclusive or for two integers say a and b. To find XOR, we will first find the binary representation of both a and b. Lets do this also by example. Suppose a = 7 and b = 10.
Bitwise XOR (exclusive or) "^" is an operator in Java that provides the answer '1' if both of the bits in its operands are different, if both of the bits are same then the XOR operator gives the result '0'. XOR is a binary operator that is evaluated from left to right.
The XOR logical operation, exclusive or, takes two boolean operands and returns true if, and only if, the operands are different. Conversely, it returns false if the two operands have the same value.
If you mean to do this bitwise you need to use the Double
utility functions to get long
representations and then convert back to a double at the end:
double c = Double.longBitsToDouble(
Double.doubleToRawLongBits(a) ^ Double.doubleToRawLongBits(b));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With