简体   繁体   English

我想使用64位整数可以接受的全部范围。 如何在x86 Visual Studio 2008中做到这一点?

[英]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.

相关问题 如何使用visual c ++ 2008编译64位 - How can I compile 64 bit with visual c++ 2008 如何获取该函数以接收4位二进制字符串并返回64位二进制字符串? - How can I get the function to take in a 4-bit binary string and return a 64-bit one? 如何在Visual Studio 2003中创建64位本机ATL C ++ DLL? - How do I create a 64-bit native ATL C++ DLL in Visual Studio 2003? 如何使用P / Invoke从64位C#应用程序中调用64位C ++ DLL? - How can I use P/Invoke to call a 64-bit C++ DLL from a 64-bit C# Application? 如何在 arm64 和 x86 上的 macOS 中获得 CPU 空闲驻留 - How can I get CPU idle residency in macOS on arm64 and x86 使用Visual Studio 2008从x86编译x64 dll - Compiling x64 dll from x86 using Visual Studio 2008 在Win 7 64位上在Visual Studio 2012中编写x86汇编代码时遇到的问题 - problems in writing x86 assembly code in visual studio 2012 on win 7 64 bit 如何为x86和x64体系结构构建boost通用库? - How do I build boost universal libraries for x86 and x64 architecture? 如何在64位x86环境中编译32位x86应用程序? - How to compile 32bit x86 application in 64bit x86 environment? x86上的两个128位整数的高效乘法/除法(无64位) - Efficient Multiply/Divide of two 128-bit Integers on x86 (no 64-bit)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM