整数转换排名和促销 [英] integer conversion rank and promotion
问题描述
阅读整数提升和整数转换排名我发现这是 link
Reading about the integer promotion and integer conversion rank I found this link
- 1.如果两个操作数具有相同的类型,然后不需要进一步转换。
- 2.否则,如果两个操作数都有有符号整数类型或两者都有无符号整数类型,则操作数的类型为较小整数
转换等级转换为具有更大
等级的操作数的类型。
3.否则,如果具有无符号整数类型的操作数的等级大于或等于等级对于另一个操作数的类型,然后
将带有符号整数类型的操作数转换为带有无符号整数类型的
操作数的类型。
- 1.If both operands have the same type, then no further conversion is needed.
- 2.Otherwise, if both operands have signed integer types or both have unsigned integer types, the operand with the type of lesser integer conversion rank is converted to the type of the operand with greater rank.
3.Otherwise, if the operand that has unsigned integer type has rank greater or equal to the rank of the type of the other operand, then the operand with signed integer type is converted to the type of the operand with unsigned integer type.
4.否则,如果带有符号整数类型的操作数的类型可以表示t的所有值他使用无符号
整数类型的操作数类型,然后将带有无符号整数类型的操作数转换为
到带有符号整数类型的操作数的类型。
4.Otherwise, if the type of the operand with signed integer type can represent all of the values of the type of the operand with unsigned integer type, then the operand with unsigned integer type is converted to the type of the operand with signed integer type.
5.否则,两个操作数都将转换为无符号整数类型,对应于带有符号整数类型的操作数类型。
5.Otherwise, both operands are converted to the unsigned integer type corresponding to the type of the operand with signed integer type.
积分1 2 3完全清楚但我还没有提出案例4和5的例子。有人可以提供一个关于任何实现的例子吗?
The points 1 2 3 are totally clear but I still not come up with example for the case 4 and 5. Can someone provide please an example concerning any implementation ?
我知道整数转换等级是:
As I know the integer conversion rank is:
_Bool< char<简短< int<长< long long int
_Bool < char < short < int < long < long long int
与类型相关的字节大小是相等还是更高。对吗?
Whatever the size of bytes related to the types are equal or higher. Right?
关于从一种类型到另一种类型的促销或转换。添加到最低类型为零或1或左极端位的位是否会对此产生影响?
Concerning the promotion or conversion from one type to the other. Are the bits added to the lowest type zero or 1 or the left extreme bit has effect on that ?
我想知道位视图中的进程如何,特别是对于转换。
I want to know how is the process in the bits view especially for conversion.
对于整数提升,它可以毫无疑问地保存值和符号。
For the integer promotion it can always conserve the value and the sign without a doubt.
推荐答案
如果你的无符号类型的等级小于它运行的有符号类型并且它们有不同尺寸。情况5然后两个是相同的大小。
Case 4 applies if you have an unsigned type that is smaller in rank than the signed type it is operating with and they have different sizes. Case 5 then if the two are the same size.
例如,在我的系统上 int
是32位, long
是64位, long long
是64位。如果你有以下内容:
For example, on my system int
is 32-bit, long
is 64-bit, and long long
is 64-bit. If you then have the following:
unsigned int a; // range: 0 to 4294967295
long b; // range: -9223372036854775808 to 9223372036854775807
unsigned long c; // range: 0 to 18446744073709551615
long long d; // range: -9223372036854775808 to 9223372036854775807
对于涉及的表达式
和 b
, unsigned int
和 long
,任何有效的 unsigned int
都可以放在 long
中。所以 a
在这种情况下转换为 long
。
For an expression involving a
and b
, which are unsigned int
and long
, any valid unsigned int
can fit in a long
. So a
is converted to long
in that situation.
相反,对于涉及 c
和 d
的表达式, unsigned long
和 long long
, long long
无法保存的所有值unsigned长
。因此,两个操作数都转换为 unsigned long long
。
Conversely, For an expression involving c
and d
, which are unsigned long
and long long
, a long long
cannot hold all values of an unsigned long
. So both operands are converted to unsigned long long
.
关于在位级别的促销/转换期间发生的情况,我们首先假设较低等级类型小于较高等级类型,并且签名类型使用2的补码表示。
Regarding what happens during a promotion / conversion on the bit level, let's first assume that the lower rank type is smaller than the higher rank type, and that signed types use 2's complement representation.
对于从32位<$的转换c $ c> int 到64位 long
,如果值为正,则在左侧添加包含全0位的4个字节。如果值为负,则在左侧添加包含全1位的4个字节。例如,值 5
的表示形式从 0x00000005
更改为 0x0000000000000005
。对于值 -5
,表示形式从 0xfffffffb
更改为 0xfffffffffffffffb
。
For a conversion from a 32 bit int
to a 64 bit long
, if the value is positive, 4 bytes containing all 0 bits are added on the left. If the value is negative, 4 bytes containing all 1 bits are added on the left. For example, the representation of value 5
changes from 0x00000005
to 0x0000000000000005
. For the value -5
, the representation changes from 0xfffffffb
to 0xfffffffffffffffb
.
这篇关于整数转换排名和促销的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!