本文讨论jiffies回转的比较函数:
#define time_after(a, b) \ ((long)(b) - (long)(a) < 0)
写了个例子如下:
#include <stdio.h> #include <stdint.h> #define time_after(a, b) \ ((long)(b) - (long)(a) < 0) int main() { unsigned long la = 0x8000000000000000; unsigned long lb; lb = la - 1; // la--; // lb--; printf("la: %llu, %lld, %d\n", la, la, sizeof(la)); printf("lb: %llu, %lld, %d\n", lb, lb, sizeof(lb)); if (time_after(la, lb)) { printf("la(%llu) after lb(%llu);\n", la, lb); printf("la(%lld) after lb(%lld);\n", la, lb); } else { printf("la(%llu) not after lb(%llu);\n", la, lb); printf("la(%lld) not after lb(%lld);\n", la, lb); } return 0; }
运行结果如下:
9223372036854775808 就是0x800000000000, 把它加一就变成负数了。
参考一下,int8的正负号序列:
于是这里有个问题:
9223372036854775807 - -9223372036854775808 = 18446744073709551615
为什么可以成立。
因为18446744073709551615 转化成有符号数 -1了。
另外回转有效的前题是,回转值小于1/2的max(uint64)。
标签:printf,lb,la,after,llu,jiffies,time,lld From: https://www.cnblogs.com/hugetong/p/18451691