现在的位置: 首页 > 综合 > 正文

64位整型和32位整型,表示微妙级别的时间

2013年12月05日 ⁄ 综合 ⁄ 共 1583字 ⁄ 字号 评论关闭

若使用64位整型和32位整型,能表示微妙级别的时间多长呢?

#define __STDC_FORMAT_MACROS

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <inttypes.h>
#include <iostream>
using namespace std;

int main(int argc, char** argv){
    if(true){
        u_int64_t v=0xFFFFFFFFFFFFFFFF;
        printf("64bits=%"PRIu64"us, %"PRIu64"ms, %"PRIu64"s, %"PRIu64"hour, %"PRIu64"day, %"PRIu64"year\n", v, v/1000,  v/1000000, v/1000000/3600, v/1000000/3600/24, v/1000000/3600/24/365);
    }
    if(true){
        u_int32_t v=0xFFFFFFFF;
        printf("32bits=%"PRIu64"us, %"PRIu64"ms, %"PRIu64"s, %"PRIu64"hour, %"PRIu64"day, %"PRIu64"year\n", v, v/1000,  v/1000000, v/1000000/3600, v/1000000/3600/24, v/1000000/3600/24/365);
    }
    return 0;
}

[winlin@dev6 temp]$ g++ test.cpp -o int64-day
[winlin@dev6 temp]$ ./int64-day 
64bits=18446744073709551615us, 18446744073709551ms, 18446744073709s, 5124095576hour, 213503982day, 584942year

32bits=4294967295us, 4294967ms, 4294s, 1hour, 0day, 0year

若用来表示毫秒呢:

#define __STDC_FORMAT_MACROS

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <inttypes.h>
#include <iostream>
using namespace std;

int main(int argc, char** argv){
    if(true){
        u_int64_t v=0xFFFFFFFFFFFFFFFF;
        printf("64bits=%"PRIu64"ms, %"PRIu64"s, %"PRIu64"hour, %"PRIu64"day, %"PRIu64"year\n", v, v/1000,  v/1000/3600, v/1000/3600/24, v/1000/3600/24/365);
    }
    if(true){
        u_int32_t v=0xFFFFFFFF;
        printf("32bits=%"PRIu64"ms, %"PRIu64"s, %"PRIu64"hour, %"PRIu64"day, %"PRIu64"year\n", v, v/1000,  v/1000/3600, v/1000/3600/24, v/1000/3600/24/365);
    }
    return 0;
}

[winlin@dev6 temp]$ g++ test.cpp -o int64-day
[winlin@dev6 temp]$ ./int64-day 
64bits=18446744073709551615ms, 18446744073709551s, 5124095576030hour, 213503982334day, 584942417year
32bits=4294967295ms, 4294967s, 1193hour, 49day, 0year

抱歉!评论已关闭.