Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XORing two doubles in Java

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?

like image 588
Eternal Noob Avatar asked Nov 18 '10 02:11

Eternal Noob


People also ask

What is XOR of two numbers in Java?

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.

What is XOR of two numbers?

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.

How bitwise XOR operator works in Java?

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.

What is XOR method in Java?

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.


1 Answers

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));
like image 91
Mark Elliot Avatar answered Nov 09 '22 14:11

Mark Elliot