1

OK, I'm trying to print the decimal/hexadecimal version of a relatively big unsigned long long and the results I'm getting are quite weird...

The code :

unsigned long long a = 1llu<<63;

printf("decimal = %llu\n",a);
printf("hexadecimal = %llx\n",a);

The output :

decimal = 9223372036854775808
hexadecimal = 8000000000000000

Now, here's what :

  • The hexadecimal output is correct.
  • The decimal output is not (should be 9223372036854780000)

Why's that happening? What am I doing wrong???

Dr.Kameleon
  • 22,532
  • 20
  • 115
  • 223

1 Answers1

8

Since five is not a factor of any power of two, no power of two ends in zero. Your other source which gave 9223372036854780000 is incorrect.

Potatoswatter
  • 134,909
  • 25
  • 265
  • 421
  • 1
    My other source is Mac OS X's Programmer's calculator. But, yep, your mathematical point of view definitely makes sense. – Dr.Kameleon Dec 13 '12 at 04:15
  • 1
    @Dr.Kameleon Punching "1 << 63" gives a number ending in 808. Using the scientific calculator's 2^x button for 2^63 then switching to programmer mode results in 9,223,372,036,854,776,000. Not sure exactly what you did. – Potatoswatter Dec 13 '12 at 04:17
  • I'm really confused. Just switched to `Programmer` view. `1` `X< – Dr.Kameleon Dec 13 '12 at 04:23
  • 4
    Congratulations, you have found a bug in the Mac calculator. – brian beuning Dec 13 '12 at 04:25
  • 1
    Mac calculator is probably computing with double precision floating point math, and using a low-quality print-to-decimal algorithm that cannot produce the exact value (note that the exact value **is** representable in IEEE double). – R.. GitHub STOP HELPING ICE Dec 13 '12 at 04:59