超过 4 个字节的整数是什么类型?
我必须使用可能超过 4 个字节的无符号整数,我应该使用什么类型?
PS 抱歉“菜鸟主义”,但就是这样:D
注意:我需要整数,因为我必须做除法并且只关心整数部分,这样 int 很有用
I have to use unsigned integers that could span to more than 4 bytes, what type should I use?
PS Sorry for the "noobism" but that's it :D
NB: I need integers because i have to do divisions and care only for the integer parts and this way int are useful
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
只需包含即可 并使用 int64_t 和 uint64_t (因为你想要无符号,所以你想要 uint64_t )。
该标头还有其他几个有用的变体,例如最小变体(uint_least64_t 是至少 64 位的类型)和快速变体(uint_fast64_t 是至少 64 位的最快整数类型)。 同样非常有用的是 intptr_t/uintptr_t (对于 void * 指针来说足够大)和 intmax_t/uintmax_t (最大类型)。
如果由于某种原因你的编译器没有(因为 IIRC 它是一种 C 标准,而不是 C++ 标准),您可以使用 Boost 的 boost/cstdint.hpp (即使您确实有也可以使用它,因为在这种情况下它应该简单地转发到编译器的标头)。
Simply include <stdint.h> and use int64_t and uint64_t (since you want unsigned, you want uint64_t).
There are several other useful variants on that header, like the least variants (uint_least64_t is a type with at least 64 bits) and the fast variants (uint_fast64_t is the fastest integer type with at least 64 bits). Also very useful are intptr_t/uintptr_t (large enough for a void * pointer) and intmax_t/uintmax_t (largest type).
And if for some reason your compiler doesn't have a <stdint.h> (since IIRC it's a C standard, not a C++ one), you can use Boost's boost/cstdint.hpp (which you can use even if you do have a <stdint.h>, since in that case it should simply forward to the compiler's header).
long long,64 位整数...
long long, 64 bit integer... here you can find some reference about the data types and ranges...
选择:
long long
(–9,223,372,036,854,775,808 到 9,223,372,036,854,775,807)unsigned long long
:(0 到 18,446,744,073,709,551,615)Take your pick:
long long
(–9,223,372,036,854,775,808 to 9,223,372,036,854,775,807)unsigned long long
: (0 to 18,446,744,073,709,551,615)如果您需要非常长的整数(任意精度),您还可以尝试 gmp 库,它还提供了一个 C++ 类基于接口。
If you need really long integers (arbitrary precision), you could also try the gmp library, which also provides a C++ class based interface.
unsigned long long - 至少 64 位长
unsigned long long - it is at least 64 bits long