Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 需要澄清(u/i)int\u fastN\t_C_Memory_C99_Unsigned_Signed - Fatal编程技术网

C 需要澄清(u/i)int\u fastN\t

C 需要澄清(u/i)int\u fastN\t,c,memory,c99,unsigned,signed,C,Memory,C99,Unsigned,Signed,我读了很多关于最快最小宽度整数类型的解释,但我不明白什么时候使用这些数据类型 我的理解是: 在32位机器上 uint__至少16________________________________ 1. uint_least16_t small = 38; 2. unsigned short will be of 16 bits so the value 38 will be stored using 16 bits. And this will take up 16 bits of memor

我读了很多关于最快最小宽度整数类型的解释,但我不明白什么时候使用这些数据类型

我的理解是: 在32位机器上

uint__至少16________________________________

 1. uint_least16_t small = 38;
 2. unsigned short will be of 16 bits so the value 38 will be stored using 16 bits. And this will take up 16 bits of memory.
 3. The range for this data type will be 0 to (2^N)-1 , here N=16.
uint_fast16_t可以是typedef到无符号int

 1. uint_fast16_t  fast = 38; 
 2. unsigned int will be of 32 bits so the value 38 will be stored using 32 bits. And this will take up 32 bits of memory.
 3. what will be the range for this data type ?
    uint_fast16_t => uint_fastN_t , here N = 16
    but the value can be stored in 32 bits so IS it 0 to (2^16)-1 OR 0 to (2^32)-1 ?
    how can we make sure that its not overflowing ? 
    Since its a 32 bit, Can we assign >65535 to it ?

    If it is a signed integer, how signedness is maintained.
    For example int_fast16_t = 32768;
    since the value falls within the signed int range, it'll be a positive value.

uint\u fast16\t
是最快的无符号数据类型,至少有16位。在某些机器上,它将是16位,而在其他机器上,它可能会更多。如果您使用它,您应该小心,因为给出0xFFFF以上结果的算术运算可能在不同的机器上有不同的结果

在某些机器上,是的,您可以在其中存储大于0xFFFF的数字,但在您的设计中不应该依赖于这一点,因为在其他机器上这是不可能的

通常,
uint\u fast16\u t
类型将是
uint16\u t
uint32\u t
uint64\u t
的别名,您应该确保代码的行为不依赖于所使用的类型

我想说,如果您需要编写既快速又跨平台的代码,您应该只使用
uint\u fast16\t
。大多数人应该坚持使用
uint16\u t
uint32\u t
uint64\u t
,这样在将代码移植到另一个平台时就不会有太多潜在问题需要担心了

一个例子 下面是一个你可能会遇到麻烦的例子:

bool bad_foo(uint_fast16_t a, uint_fast16_t b)
{
    uint_fast16_t sum = a + b;
    return sum > 0x8000;
}
如果调用上述函数时,
a
为0x8000,
b
为0x8000,则在某些机器上,
sum
将为0,而在其他机器上,它将为0x10000,因此该函数可能返回true或false。现在,如果您可以证明
a
b
的总和永远不会大于0xFFFF,或者如果您可以证明在这些情况下忽略了
bad_foo
的结果,那么这个代码就可以了

相同代码的更安全实现(我认为)应该在所有机器上以相同的方式运行,应该是:

bool good_foo(uint_fast16_t a, uint_fast16_t b)
{
    uint_fast16_t sum = a + b;
    return (sum & 0xFFFF) > 0x8000;
}

+1个不错的答案,但我建议
return(sum>0x8000)| |(sum可移植测试,用于测试
sum>0x8000
<代码>(总和&0xFFFF)>0x8000a=0xFFFF,b=1
,code>失败,但该网站上有人提到固定宽度整数类型在C中是可选的。@David是因为使用机器字大小的类型会更有效。这就像说“让表达式以更快的速度计算,我们会处理结果”@Harsha,如果你的编译器支持C99,那么你我认为你可以包括
stdint.h
,并访问像uint16\t这样的固定宽度类型。你为什么说它们是可选的?你有什么问题吗?“我的回答有什么地方不对吗?”哈萨,对于你的第二个评论,我会说答案是肯定的。在32位处理器上,在32位寄存器中存储16位整数可能会更快,并让程序员(而不是编译器)担心何时需要屏蔽高位。这就是
uint\u fast16\t
允许我们做的事情。