开发者

Integers greater than 4294967295 on 32-bit Windows

开发者 https://www.devze.com 2023-03-05 00:35 出处:网络
I\'m trying to get to开发者_运维知识库 grips with C++ basics by building a simple arithmetic calculator application. Right now I\'m trying to figure out how to make it capable of dealing with integers

I'm trying to get to开发者_运维知识库 grips with C++ basics by building a simple arithmetic calculator application. Right now I'm trying to figure out how to make it capable of dealing with integers greater than 4294967295 on 32-bit Windows. I know that Windows' integrated Calculator is capable of this. What have I missed?

Note that this application should be compilable with both MSVC compiler and g++ (MinGW/GCC).

Thank you.


If you want to be both gcc and msvc compatible use <stdint.h>. It's source code compatible with both.

You probably want uint64_t for this. It will get you up to 18,446,744,073,709,551,615.

There are also libraries to get you up to integers as large as you have memory to handle as well.


Use __int64 to get 64-bit int calculations in Visual C++ - not sure if GCC will like this, though.

You could create a header file that typedefs (say) MyInt64 to the appropriate thing for each compiler. Then you can work internally with MyInt64, and the compiled code will be correct for each target. This is a pretty standard way of supporting different target compilers on one source codebase.

afai can tell, long long would work OK for both, but I have not used GCC so YMMV - see here for GCC info and here for Visual C++.


You could also create a "Large Number" class that would basically store the value across multiple variables in one form or another


There are different solutions, if 2^64 is big enough for you, you can use a 64 bit integer type (these are implementation dependent, so search for your particular compiler). On the other hand, if you want to be able to handle any number, you will have to use or implement a BigInteger type that encapsulates it. The implementation is an interesting exercise... basically use a vector of smaller type, operate on each subelement and then merge and normalize the result.

0

精彩评论

暂无评论...
验证码 换一张
取 消