两个无符号类型想减结果存储的临时变量还是无符号类型
1 #include <stdio.h> 2 int main() 3 { 4 unsigned int dwVal = 0; 5 unsigned __int64 n64Val = 1; 6 if( dwVal - n64Val >= 20) 7 { 8 printf("%I64d\n",dwVal - n64Val); //执行这行,输出 -1 9 } 10 return 0; 11 }
个人分析:
在做想减操作(dwVal - n64Val)时,会将dwVal和n64Val都转换为unsigned __int64,
两者想减的结果为-1,存储为0xFFFFFFFFFFFFFFFF,当成unsigned __int64使用
与 后面的数字(20)进行比较,所以计算结果为true,而打印输出的结果是 __int64类型
对应值为-1
进一步验证:
1 #include <iostream> 2 int main() 3 { 4 unsigned int dwVal = 0; 5 unsigned __int64 n64Val = 1; 6 unsigned __int64 n64Val2 = 0xffffffffffffffff; 7 if( dwVal - n64Val >= n64Val2) 8 { 9 printf("%I64d\n",dwVal - n64Val); //执行这行,输出 -1 10 } 11 n64Val2 = dwVal - n64Val; //n64Val2 = 0xffffffffffffffff 12 system("pause"); 13 return 0; 14 }
1 #include <iostream> 2 int main() 3 { 4 unsigned int dwVal = 0; 5 unsigned __int64 n64Val = 2; 6 unsigned __int64 n64Val2 = 0xffffffffffffffff; 7 if( dwVal - n64Val >= n64Val2) 8 { 9 printf("%I64d\n",dwVal - n64Val); //执行不到这里 10 } 11 n64Val2 = dwVal - n64Val; //n64Val2 = 0xfffffffffffffffe,小于n64Val2 12 system("pause"); 13 return 0; 14 }
有感于GetTickCount() 49.7天后计数置零
_LogTick = ::GetTickCount64(); //DoSomething; if(::GetTickCount() - _LogTick >= LOG_TICK_MAX) { printf("%I64d\n",::GetTickCount() - _LogTick); }