Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bitwise Not for double in Java

Tags:

java

bit

In Java, the bitwise not operator (~) inverts all bits of an integer or long. But why is this not possible for a double? Is there any method of using this operation on a double? I am writing my own programming language in Java (this is NOT a homework assignment), and I believe that bitwise should be included for doubles.

Any ways of using bitwise not on doubles?

EDIT: This is not a duplicate of How to perform a bitwise operation on floating point numbers, because I am using Java as opposed to the discussed C. I also know that there is no possible way using the syntax of the language, but the answers there seem to cast the double to an int. I need to keep the double as it is, without changing it to an int or long.

EDIT: The bitwise not isn't what I thought it would be. It seems to perform *-1 - 1 on the input. Therefore, my question now becomes:

I wish to turn a double into binary (see my other question ), invert all of the bits (0 to 1 and 1 to 0), then convert it back to a double.

like image 691
hyper-neutrino Avatar asked Apr 26 '26 07:04

hyper-neutrino


1 Answers

Java didn't supply the ~ operator on the floating-point types float and double because such an operation isn't meaningful.

You can simulate it with this method:

public static double performNotOnDouble(double d)
{
    return Double.longBitsToDouble(~Double.doubleToLongBits(d));
}

But the results don't have any real meaning:

0.0: NaN
1.0: -3.9999999999999996
10.0: -0.43749999999999994
-1.0: 3.9999999999999996
0.01: -440.31999999999994
3.141592653589793: -1.4292036732051032
2.718281828459045: -1.6408590857704772
Infinity: -2.225073858507201E-308
-Infinity: 2.225073858507201E-308
NaN: -1.1125369292536E-308

There is no point to taking the bit-complement of a floating-point type, because the result doesn't have any meaning.

like image 143
rgettman Avatar answered Apr 28 '26 19:04

rgettman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!