简体   繁体   English

uint8_t-8位保证

[英]uint8_t - 8 bits guarantee

How & where is it guaranteed that an uint8_t is 8-bits? 如何以及在哪里保证uint8_t为8位?

Digging into N3242 - "Working Draft, Standard for Programming Language C++", section 18.4.1 < cstdint > synopsis says - 深入研究N3242-“ C ++编程语言标准工作草案”,第18.4.1节<cstdint>内容提要-

       `typedef unsigned integer type uint8_t; // optional`

So, in essence, a C++ standard conforming library is not needed to define uint8_t at all. 因此,从本质上讲,完全不需要C ++标准的库来定义uint8_t

Update: Probably am just asking, which line of the standard says that uintN_t types are N bits? 更新:可能只是在问,标准的哪一行说uintN_t类型是N位?

From C++: 从C ++:

18.4.1 Header synopsis 18.4.1标头简介

... The header defines all functions, types, and macros the same as 7.18 in the C standard. ...标头定义与C标准中的7.18相同的所有函数,类型和宏。 ... ...

From C: 从C:

7.20.1.1 Exact-width integer types 7.20.1.1精确宽度整数类型

1 The typedef name intN_t designates a signed integer type with width N , no padding bits, and a two's complement representation. 1 typedef名称intN_t指定一个带符号的整数类型,其宽度为N,没有填充位,并且用二进制补码表示。 Thus, int8_t denotes such a signed integer type with a width of exactly 8 bits. 因此,int8_t表示这样的带符号整数类型,其宽度恰好为8位。

2 The typedef name uintN_t designates an unsigned integer type with width N and no padding bits. 2 typedef名称uintN_t指定宽度为N并且没有填充位的无符号整数类型。 Thus, uint24_t denotes such an unsigned integer type with a width of exactly 24 bits. 因此,uint24_t表示这种无符号整数类型,其宽度恰好为24位。

3 These types are optional. 3这些类型是可选的。 However, if an implementation provides integer types with widths of 8, 16, 32, or 64 bits, no padding bits, and (for the signed types) that have a two's complement representation, it shall define the corresponding typedef names. 但是,如果实现提供的整数类型的宽度为8、16、32或64位,没有填充位,并且(对于带符号的类型)具有二进制补码表示,则它应定义相应的typedef名称。


So, in essence, a C++ standard conforming library is not needed to define uint8_t at all. 因此,从本质上讲,完全不需要C ++标准的库来定义uint8_t。

Correct. 正确。 As Nikos mentioned (+1), you just need an alternative when/if the typedef is not present/declared. 正如Nikos提到的(+1),当/如果不存在/未声明typedef ,您只需要一个替代方法。

The <stdint.h> types are defined with reference to the C99 standard. <stdint.h>类型是参考C99标准定义的。

C99 draft N869 §7.17.1.1/2 : C99 N869草案7.17.1.1/2
“The typedef name uint N _t designates an unsigned integer type with width N . “ typedef名称uint N _t指定宽度为N的无符号整数类型。 Thus, uint24_t denotes an unsigned integer type with a width of exactly 24 bits.” 因此, uint24_t表示宽度为24位的无符号整数类型。”

If a type is defined by <stdint.h> , then so are its associated macros, and if the type is not defined, then neither are its associated macros, by C99 §7.18/4. 如果类型由<stdint.h>定义,则其关联的宏也是如此,并且如果未定义该类型,则其关联的宏也不会由C99§7.18/ 4定义。

Thus, you can use the existence or not of the macro UINT8_MAX (required by C99 §7.18.2.1) to check for the presence or absence of the uint8_t type definition. 因此,可以使用是否存在宏UINT8_MAX (由C99§7.18.2.1要求)来检查是否存在uint8_t类型定义。

uint8_t is guaranteed to be 8 bits wide. uint8_t保证为8位宽。 If it doesn't exist, then obviously you can't use it. 如果它不存在,那么显然您将无法使用它。 But if it's there, it's 8 bits. 但是,如果有,那是8位。 There's no possibility where it's provided but is not 8 bits. 没有提供它的可能性,但是它不是8位的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM