uint32_t vs uint_fast32_t vs uint_least32_t [英] uint32_t vs uint_fast32_t vs uint_least32_t

查看:223
本文介绍了uint32_t vs uint_fast32_t vs uint_least32_t的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 stdint.h 中看到了不同类型的整数定义。我将以无符号32位整数为例。


  1. uint32_t 显然是32位的无符号整数。这是我一直使用的那个。


  2. uint_fast32_t uint_least32_t :与 uint32_t 的区别是什么?我何时应该使用它们而不是 uint32_t


现在,我看到 uintX_t 其中X是24 ,40,48和56.在我的代码中,我必须使用48和56位整数。举个例子,我想 uint24_t 定义如下:

  struct uint24_t {unsigned int the_integer:24; }; 

我是对的吗?并且,您是否建议我使用 uint48_t 作为我的48位无符号整数,还是应该使用正常的 uint64_t



感谢您的解释。

解决方案


与uint32_t有什么区别


uint_fast32_t 是at的无符号类型至少32位(以某种通用方式)是最快的这种类型。 快速意味着在给定选择的情况下,实现者可能会选择体系结构具有算术,加载和存储指令的大小。它不是任何特定基准测试的赢家。



uint_least32_t 是最小的无符号类型,至少32位。 / p>

uint32_t 是一种正好32位且没有填充的类型,如果存在任何此类类型。


我是对的吗?


没有。如果 uint24_t 完全存在,则它是整数类型,而不是 struct 。如果在此实现中没有无符号整数类型的24位,那么它就不存在。



因为 unsigned long 要求至少为32位, uint24_t 唯一的标准类型可能是 char 的别名, unsigned char unsigned short unsigned int 。或者,它可以是扩展类型(即,实现提供的整数类型,不是标准中任何定义的整数类型)。


您是否建议我使用uint48_t作为我的48位无符号整数?


如果它存在且大小合适希望你不妨使用它。但是,它不会存在于很多实现中,因此它仅适用于非可移植代码。如果您必须处理精确的48位整数是特定于平台的原因,那就没问题了。



确切的16位,32位和64位类型在技术上也是可选的,但是如果实现具有合适的整数类型,则它们必须存在。 合适不仅意味着存在精确的N位无符号类型而没有填充位,而且相应的有符号类型没有填充位并且使用2的补码表示。在实践中,这是非常接近任何地方,你通过使用它们中的任何一个来限制可移植性。为了获得最大的可移植性,您应该使用 uint_least32_t uint_fast32_t 优先于 uint32_t 。哪一个取决于您是否更关心速度或尺寸。根据我的经验,很少有人会烦恼,因为没有32位整数类型的平台已经非常奇怪,以至于大多数人都不关心他们的代码是否运行。


I saw different types of definition of an integer in stdint.h. I'll take unsigned 32-bit integer as an example.

  1. uint32_t means clearly an unsigned integer of 32 bits. That's the one I always use.

  2. uint_fast32_t and uint_least32_t : What's the difference with uint32_t and when should I use them instead of uint32_t ?

And now, I saw uintX_t where X is 24, 40, 48 and 56. It happens in my code that I have to work with 48 and 56-bit integers. As an example, I suppose uint24_t is define as something like this :

struct uint24_t { unsigned int the_integer : 24; };

Am I right ? And, Will you suggest me to use uint48_t for my 48-bit unsigned integers or should I use the normal uint64_t ?

Thanks for your explanations.

解决方案

what's the difference with uint32_t

uint_fast32_t is an unsigned type of at least 32 bits that is (in some general way) the fastest such type. "fast" means that given a choice, the implementer will probably pick the size for which the architecture has arithmetic, load and store instructions. It's not the winner of any particular benchmark.

uint_least32_t is the smallest unsigned type of at least 32 bits.

uint32_t is a type of exactly 32 bits with no padding, if any such type exists.

Am I right?

No. If uint24_t exists at all then it is an integer type, not a struct. If there is no unsigned integer type of 24 bits in this implementation then it does not exist.

Since unsigned long is required to be at least 32 bits, the only standard types that uint24_t could possibly ever be an alias for are char, unsigned char, unsigned short and unsigned int. Alternatively it could be an extended type (that is, an integer type provided by the implementation that is not any of the defined integer types in the standard).

Will you suggest me to use uint48_t for my 48-bit unsigned integers?

If it exists and is the size you want then you might as well use it. However, it will not exist on very many implementations, so it's only suitable for use in non-portable code. That's OK provided the reason you have to deal with exact 48-bit integers is platform-specific.

The exact 16, 32 and 64 bit types are also technically optional, but they are required to exist if the implementation has suitable integer types. "Suitable" means not only that there is an exact N bit unsigned type with no padding bits, but also that the corresponding signed type has no padding bits and uses 2's complement representation. In practice this is so close to everywhere that you limit portability very little by using any of them. For maximum portability though you should use uint_least32_t or uint_fast32_t in preference to uint32_t. Which one depends on whether you care more about speed or size. In my experience very few people bother, since platforms that don't have a 32 bit integer type are already so weird that most people don't care whether their code runs on it or not.

这篇关于uint32_t vs uint_fast32_t vs uint_least32_t的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆