C++ 中是否有实际的 8 位整数数据类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35246212/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 14:29:32  来源:igfitidea点击:

Is there an actual 8-bit integer data type in C++

c++integer

提问by tej-kweku

In c++, specifically the cstdint header file, there are types for 8-bit integers which turn out to be of the char data type with a typedef. Could anyone suggest an actual 8-bit integer type?

在 c++ 中,特别是 cstdint 头文件中,有一些 8 位整数类型,它们是带有 typedef 的 char 数据类型。谁能建议一个实际的 8 位整数类型?

回答by dlmeetei

Yes, you are right. int8_tand uint8_tare typedefto charon platforms where 1 byte is 8 bits. On platforms where it is not, appropriate definition will be given.

你是对的。int8_t并且uint8_ttypedefchar平台上,其中1个字节为8位。在没有它的平台上,将给出适当的定义。

Following answer is based on assumption that char is 8 bits

以下答案基于假设 char 是 8 bits

charholds 1 byte, which may be signedor unsignedbased on implementation.

char持有 1 个字节,这可能是signedunsigned基于实现。

So int8_tis signed charand uint8_tis unsigned char, but this will be safe to use int8_t/uint8_t as actual 8-bit integer without relying too much on the implementation.

所以,int8_tsigned charuint8_tunsigned char,但是这将是安全的使用中int8_t / uint8_t作为实际的8位整数,而不过分依赖于实现。

For a implementer's point of view, typedeffing where char is 8 bits makes sense.

对于实现者的观点,typedeffing 其中 char 是 8 位是有意义的。

Having seen all this, It is safe to use int8_tor uint8_tas real 8 bit integer.

看到这一切后,可以安全地使用int8_toruint8_t作为真正的 8 位整数。