整数转换排名和提升 [英] integer conversion rank and promotion

查看:18
本文介绍了整数转换排名和提升的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

阅读有关整数提升和整数转换排名的信息,我发现了这个 链接

Reading about the integer promotion and integer conversion rank I found this link

  • 1.如果两个操作数的类型相同,则不需要进一步转换.

  • 1.If both operands have the same type, then no further conversion is needed.

2.否则,如果两个操作数都是有符号整数类型或者都是无符号整数类型,则操作数类型为小整数转换等级转换为具有更大的操作数的类型排名.

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.否则,如果具有无符号整数类型的操作数的秩大于或等于另一个操作数的类型的秩,则带符号整数类型的操作数转换为无符号整数类型的操作数.

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.否则,如果带符号整数类型的操作数可以表示无符号操作数类型的所有值整数类型,然后转换无符号整数类型的操作数为有符号整数类型的操作数的类型.

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

字符短整数<长长长整型

_Bool < char < short < int < long < long long int

无论与类型相关的字节大小相等或更高.对吧?

Whatever the size of bytes related to the types are equal or higher. Right?

关于从一种类型到另一种类型的提升或转换.添加到最低类型 0 或 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

对于涉及 ab 的表达式,它们是 unsigned intlong,任何有效的 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.

反之,对于一个涉及 cd 的表达式,它们是 unsigned longlong long,一个long long 不能保存 unsigned long 的所有值.所以两个操作数都被转换为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 位 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屋!

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