[英]I want to use the full range that the 64-bit integer can take. How can I do it in x86 Visual Studio 2008?
I included the stdint.h in my solution and used uint64_t , but the result was not what i wanted. 我在解决方案中包含了stdint.h并使用了uint64_t ,但结果不是我想要的。 Here is the code that i used. 这是我使用的代码。
#include <stdio.h>
#include "./stdint.h"
void main (void)
{
//-- to get the maximum value that the 32-bit integer can take
unsigned int test = 0 ;
test-- ;
//-- to see if the 64-bit integer can take the value that the 32-bit integer can't take
uint64_t test2 = test ;
test2++ ;
printf("%u\n", test) ;
printf("%u\n", test2) ;
while(1) { }
}
And Here is the result. 这就是结果。
4294967295
0
I want to use the full range that the 64-bit integer can take. 我想使用64位整数可以接受的全部范围。 How can i do it in x86 Visual Studio 2008? 如何在x86 Visual Studio 2008中做到这一点? For your information, i'm using a 32-bit windows 7. 供您参考,我使用的是32位Windows 7。
The %u
format specifier for printf
is for unsigned integers. printf
的%u
格式说明符用于无符号整数。 Use %llu
. 使用%llu
。
Since this is C++ though, you might as well use the type-safe std::cout
to avoid programmer error: 由于这是C ++,因此您最好使用类型安全的std::cout
以避免程序员错误:
std::cout << test2;
Use: 采用:
#include <inttypes.h>
/* ... */
printf("%" PRIu64 "\n", test2);
to print an uint64_t
value. 打印一个uint64_t
值。
u
conversion specifier is used to print an unsigned int
value. u
转换指定用于打印的unsigned int
值。
Note that the PRIu64
macro is a C macro. 请注意, PRIu64
宏是C宏。 In C++ the macro is not present and you may have to use %llu
conversion specification. 在C ++中,该宏不存在,您可能必须使用%llu
转换规范。
由于您也将其标记为C ++,因此我将添加一种避免类型不匹配的明显方法,就像您在printf
遇到的那样:改用ostream:
std::cout << test2;
In a book that i have, it says that program always convert any variables to integer when it calculates. 在我有一本书中,它说程序在计算时总是将任何变量转换为整数。 Is it possible to make the default integer 64-bit? 是否可以将默认整数设置为64位?
This is 1) not correct, and 2) no, you can't tell (most) compilers what size int
you want. 这是1)不正确,并且2)不,您无法告诉(大多数)编译器所需的int
大小。 You could of course use something like: 您当然可以使用类似:
typedef Int int64_t;
typedef Uint uint64_t;
But you can't rename int
to something other than what the compiler things it should be - which may be 16, 32, 64, 36, or 72 bits - or some other number >= 16. 但是您不能将int
重命名为编译器应具有的其他名称-可能是16、32、64、36或72位-或其他一些数字== 16。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.