据我所知,“自然大小”是特定硬件最有效处理的整数的宽度。使用时short
在数组或算术运算中,short
整数必须首先转换为int
.
Q: What exactly决定这个“自然尺寸”?
我不是在寻找简单的答案,例如
如果它具有 32 位架构,则其自然大小为 32 位
我想了解why这是最有效的,并且why a short
在对其进行算术运算之前必须进行转换。
额外问题:当对 a 进行算术运算时会发生什么?long
整数?
一般来说,每种计算机体系结构的设计都使得某些字体大小提供最有效的数值运算。具体的大小取决于架构,编译器会选择合适的大小。关于为什么硬件设计者为特定硬件选择某些尺寸的更详细解释超出了 stckoverflow 的范围。
A short
大多数人会被提升到int
在执行积分运算之前,因为这就是 C 中的方式,而 C++ 继承了该行为,几乎没有理由更改它,这可能会破坏现有代码。我不确定它最初被添加到 C 中的原因,但可以推测它与“default int”相关,如果没有指定类型int
由编译器假设。
奖励 A:从 5/9(表达式)中我们了解到:Many binary operators that expect operands of arithmetic or enumeration type cause conversions and yield result types in a similar way. The purpose is to yield a common type, which is also the type of the result. This pattern is called the usual arithmetic conversions, which are defined as follows:
然后是特别感兴趣的:
- 浮点规则在这里并不重要
Otherwise, the integral promotions (4.5) shall be performed on both operands
Then, if either operand is unsigned long the other shall be converted to unsigned long.
Otherwise, if one operand is a long int and the other unsigned int, then if a long int can represent
all the values of an unsigned int, the unsigned int shall be converted to a long int;
otherwise both operands shall be converted to unsigned long int.
Otherwise, if either operand is long, the other shall be converted to long.
总之,编译器尝试使用它可以执行的“最佳”类型来执行二进制操作,int
是使用的最小尺寸。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)