Ok, this is a weird issue :
- I’m using
unsigned long longvariables (I’ve used evenlongones, with the same effect) - I need to be able to store 64-bit integers (
sizeofreturns 8, which is fine)
However, when I’m trying to go to values like 1<<63, and perform some simple bitwise operations, I – oddly – seem to be getting negative values. Why’s that?
My test code :
unsigned long long c = 0;
c |= 1l << 56; printf("c = %lld\n",c);
c |= 1l << 63; printf("c = %lld\n",c);
Output :
c = 72057594037927936
c = -9151314442816847872
Sidenotes :
- Of course, same thing happens even if I do
c = 1l<<63directly. - All tests made on Mac OS X 10.6, and compiled using Apple’s LLVM Compiler 3.0
Any suggestions?
The
dpart of the%lldspecifier is tellingprintfthat the argument should be treated as a signed integer. Use auinstead:%llu.From the man pages: