[英]How does uint32_t guarantee 32 bits?
In most implementations, I've seen uint32_t
defined as 在大多数实现中,我已经看到
uint32_t
定义为
typedef unsigned int uint32_t;
But as I understand it ints
are not always guaranteed to be 4 bytes across all systems. 但是据我了解,在所有系统中,
ints
并不总是保证为4字节 。 So if the system has non 4-byte integers, how does uint32_t
guarantee 4? 因此,如果系统具有非4字节整数,那么
uint32_t
如何保证4?
An implementation is required to define uint32_t
correctly (or not at all if that's not possible). 需要正确定义
uint32_t
的实现(如果不可能,则完全不定义)。
If unsigned int
meets the requirements (32 bits wide, no padding bits), the implementation can define it as 如果
unsigned int
满足要求(32位宽,没有填充位),则实现可以将其定义为
typedef unsigned int uint32_t;
If it doesn't, but unsigned long
does meet the requirements, it can define it as: 如果不是,但是
unsigned long
确实满足要求,则可以将其定义为:
typedef unsigned long uint32_t;
Or it can use a different unsigned type if that's appropriate. 或者,如果合适,它可以使用其他无符号类型。
The <stdint.h>
header has to be compatible with the compiler that it's used with. <stdint.h>
标头必须与其使用的编译器兼容。 If you took a <stdint.h>
header that unconditionally defined uint32_t
as unsigned int
, and used it with a compiler that makes unsigned int
16 bits, the result would be a non-conforming implementation. 如果使用
<stdint.h>
标头无条件地将uint32_t
定义为unsigned int
,并将其与使unsigned int
16位的编译器一起使用,则结果将是不合格的实现。
Compatibility can be maintained either by tailoring the header to the compiler, or by writing the header so that it adapts to the characteristics of the compiler. 可以通过为编译器定制标头或通过编写标头以使其适应编译器的特性来保持兼容性。
As a programmer, you don't have to worry about how correctness is maintained (though of course there's nothing wrong with being curious). 作为程序员,您不必担心如何保持正确性(当然,好奇并没有错)。 You can rely on it being done correctly -- or you can complain to the provider about a serious bug.
您可以依靠它的正确完成-或者您可以向提供商投诉严重的错误。
Each C or C++ implementation that defines uint32_t
defines it in a way that works for that implementation. 每个定义
uint32_t
C或C ++实现都以适用于该实现的方式对其进行定义。 It may use typedef unsigned int uint32_t;
它可以使用
typedef unsigned int uint32_t;
only if unsigned int
is satisfactory for uint32_t
in that implementation. 仅当
unsigned int
对于该实现中的uint32_t
令人满意时。
The fact that typedef unsigned int uint32_t;
typedef unsigned int uint32_t;
appears in <stdint.h>
in one C or C++ implementation does not mean it will appear in <stdint.h>
for any other C or C++ implementation. 出现在
<stdint.h>
在一个C或C ++实现并不意味着它会出现在<stdint.h>
对任何其他C或C ++实现。 An implementation in which unsigned int
were not suitable for uint32_t
would have to provide a different <stdint.h>
. unsigned int
不适合uint32_t
必须提供一个不同的<stdint.h>
。 <stdint.h>
is part of the implementation and changes when the implementation changes. <stdint.h>
是实现的一部分,当实现更改时也会更改。
uint32_t
guarantee 32 bits?uint32_t
保证32位?
Yes. 是。
If CHAR_BIT == 16
, uint32_t
would be 2 "bytes". 如果
CHAR_BIT == 16
,则uint32_t
将为2个“字节”。 A "byte" is not always 8 bits in C. C中的“字节”并不总是8位。
The size of int
is not a major issue concerning the implementation uintN_t
. 关于实现
uintN_t
, int
的大小不是主要问题。
uintN_t
(N = 8,16,32,64) are optional non-padded types that independently exist when and if the system can support them. uintN_t
(N = 8,16,32,64)是可选的非填充类型,它们在系统能够支持它们时才独立存在。 It is extremely common to find them implemented, especially the larger ones. 找到实施它们的情况是非常普遍的,尤其是大型的。
intN_t
is similarly optional and must be 2's complement. intN_t
同样是可选的,必须为2的补码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.