Convert Double to Binary representation?

Long.toBinaryString(Double.doubleToRawLongBits(d)) appears to work just fine. System.out.println(“0: 0b” + Long.toBinaryString(Double.doubleToRawLongBits(0D))); System.out.println(“1: 0b” + Long.toBinaryString(Double.doubleToRawLongBits(1D))); System.out.println(“2: 0b” + Long.toBinaryString(Double.doubleToRawLongBits(2D))); System.out.println(“2^900: 0b” + Long.toBinaryString(Double.doubleToRawLongBits(Math.pow(2, 900)))); System.out.println(“Double.MAX_VALUE: 0b” + Long.toBinaryString(Double.doubleToRawLongBits(Double.MAX_VALUE))); /* prints: 0: 0b0 1: 0b11111111110000000000000000000000000000000000000000000000000000 2: 0b100000000000000000000000000000000000000000000000000000000000000 2^900: 0b111100000110000000000000000000000000000000000000000000000000000 Double.MAX_VALUE: 0b111111111101111111111111111111111111111111111111111111111111111 */

How to printf long long

%lld is the standard C99 way, but that doesn’t work on the compiler that I’m using (mingw32-gcc v4.6.0). The way to do it on this compiler is: %I64d So try this: if(e%n==0)printf(“%15I64d -> %1.16I64d\n”,e, 4*pi); and scanf(“%I64d”, &n); The only way I know of for doing this in a completely portable way is to use … Read more

C: Casting minimum 32-bit integer (-2147483648) to float gives positive number (2147483648.0)

Replace #define INT32_MIN (-2147483648L) with #define INT32_MIN (-2147483647 – 1) -2147483648 is interpreted by the compiler to be the negation of 2147483648, which causes overflow on an int. So you should write (-2147483647 – 1) instead. This is all C89 standard though. See Steve Jessop’s answer for C99. Also long is typically 32 bits on … Read more