c++ - Unexpected behavior from unsigned_int64; -


unsigned__int64 difference; difference=(64*33554432); printf ("size %i64u \n", difference); difference=(63*33554432); printf ("size %i64u \n", difference); 

the first # ridiculously large. second number correct answer. how changing 62 63 cause such change?

first value 18446744071562067968 second value 2113929216

sorry values 64 , 63, not 63 , 62.

unless qualified otherwise, integer literals of type int. assume on platform you're on, int 32-bit. calculation (64*33554432) overflows , becomes negative. cast unsigned __int64, gets flipped very large positive integer.

voila:

int main() {     int a1 = (64*33554432);     int a2 = (63*33554432);      printf("%08x\n", a1);    // 80000000  (negative)     printf("%08x\n", a2);    // 7e000000  (positive)      unsigned __int64 b1 = a1;     unsigned __int64 b2 = a2;      printf("%016llx\n", b1); // ffffffff80000000     printf("%016llx\n", b2); // 000000007e000000 } 

Comments

Popular posts from this blog

php - What is the difference between $_SERVER['PATH_INFO'] and $_SERVER['ORIG_PATH_INFO']? -

fortran - Function return type mismatch -

queue - mq_receive: message too long -