C99 和 C++11(以及之前的 POSIX)在 stdint
header 中引入了 least
和 fast
类型,例如int_fast16_t
或 uint_fast8_t
。
我想知道这些类型的溢出行为有什么保证。如果这些与“普通”整数类型相同(以便无符号类型在溢出时环绕),我想知道 uint_fast8_t
实际上可以映射到与 fixed-witdh 不同的任何类型uint8_t
类型,因此速度更快。
最佳答案
C11 n1570 表示
The typedef name
int_fastN_t
designates the fastest signed integer type with a width of at least N . The typedef nameuint_fastN_t
designates the fastest unsigned integer type with a width of at least N .
因此,没有任何行为是可以保证的;这些说的是 int_fastN_t
不能在 2^(n-1) - 1
... 2^(n-1) 范围内有符号溢出- 1
; uint_fastN_t
不得对小于 2^n - 1
的值进行环绕。如果您需要更精确的环绕行为,则不要使用快速类型,而是使用精确宽度类型(又名 intN_t
和 uintN_t
)。
关于c++ - 来自 stdint.h 的快速类型的溢出行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37204901/