簡體   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?

我在解決方案中包含了stdint.h並使用了uint64_t ,但結果不是我想要的。 這是我使用的代碼。

#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) { }

}

這就是結果。

4294967295
0

我想使用64位整數可以接受的全部范圍。 如何在x86 Visual Studio 2008中做到這一點? 供您參考,我使用的是32位Windows 7。

printf%u格式說明符用於無符號整數。 使用%llu

由於這是C ++,因此您最好使用類型安全的std::cout以避免程序員錯誤:

std::cout << test2;

采用:

#include <inttypes.h>

/* ... */

printf("%" PRIu64 "\n", test2);

打印一個uint64_t值。

u轉換指定用於打印的unsigned int值。

請注意, PRIu64宏是C宏。 在C ++中,該宏不存在,您可能必須使用%llu轉換規范。

由於您也將其標記為C ++,因此我將添加一種避免類型不匹配的明顯方法,就像您在printf遇到的那樣:改用ostream:

std::cout << test2;

在我有一本書中,它說程序在計算時總是將任何變量轉換為整數。 是否可以將默認整數設置為64位?

這是1)不正確,並且2)不,您無法告訴(大多數)編譯器所需的int大小。 您當然可以使用類似:

 typedef Int int64_t;
 typedef Uint uint64_t; 

但是您不能將int重命名為編譯器應具有的其他名稱-可能是16、32、64、36或72位-或其他一些數字== 16。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM