Regarding the code in the original question:
The 0
here is the int
literal 0
. ~0
is an int
with value -1
. You are initializing k
with the int
-1
. The conversion from int
to double
doesn't change the numerical value (but does change the bit pattern), and then you print out the resulting double
(which is still representing -1
).
Now, for the current question: You can't apply bitwise NOT to a double
. It's just not an allowed operation, precisely because it tends not to do anything useful to floating point values. It exists for built in integral types (plus anything with operator~
) only.
If you would like to flip all the bits in an object, the standard conformant way is to do something like this:
#include <memory>
void flip_bits(auto &x) {
// iterate through bytes of x and flip all of them
std::byte *p = reinterpret_cast<std::byte*>(std::addressof(x));
for(std::size_t i = 0; i < sizeof(x); i++) p[i] = ~p[i];
}
Then
int main() {
double x = 0;
flip_bits(x);
std::cout << x << "\n";
}
may (will usually) print some variation of nan
(dependent on how your implementation actually represents double
, of course).
Example on Godbolt