#include <inttypes.h> #include <stdio.h> int main() { uint64_t a = 90; printf("test uint64_t : %" PRIu64 "\n", a); return 0; }如果是使用C++编译器编译上述代码,有时候会报如下错误
main.cpp: In function ‘int main()’: main.cpp:9:30: error: expected ‘)’ before ‘PRIu64’ main.cpp:9:47: warning: spurious trailing ‘%’ in format [-Wformat]解决方法是在inttypes.h这个头文件前加上一个__STDC_FORMAT_MACROS宏定义
#define __STDC_FORMAT_MACROS #include <inttypes.h>
左移
对于32bit或者更短的的integer, 1左移x位就是
1≤≤x
但是对于64bit的integer, 比如
unsigned long long
或者uint64_t
, 1≤≤32
却是0. 如果需要2^32或者更大的数, 需要用1ULL≤≤32
http://cboard.cprogramming.com/c-programming/62790-bitshift-64-bit-integers.html
#include <stdio.h> #include <stdint.h> #include <inttypes.h> int main( void ) { uint64_t a = 1ULL << 32; uint64_t b = 1 << 32; printf( "a = %016"PRIx64", b = %016"PRIx64"\n", a, b); return 0; }
a = 0000000100000000, b = 0000000000000000
No comments:
Post a Comment