我有一个使用多种不同 int 类型的程序。
最常用的是uint64_t和标准int。但是我想知道我是否可以安全地在它们之间进行混合操作。
例如,我有一个uint64_t,我想向int它添加一个并将该值存储为另一个uint64_t。
做这样的事情安全吗?我必须先转换inttouint64_t才能对其使用操作吗?
我在网上真的找不到关于它的东西。它可能只是被允许的,没有人质疑它或我的谷歌查询是错误的。
无论如何,基本上我的问题是我可以混合使用不同类型的整数并进行操作吗?
我有一个使用多种不同 int 类型的程序。
最常用的是uint64_t和标准int。但是我想知道我是否可以安全地在它们之间进行混合操作。
例如,我有一个uint64_t,我想向int它添加一个并将该值存储为另一个uint64_t。
做这样的事情安全吗?我必须先转换inttouint64_t才能对其使用操作吗?
我在网上真的找不到关于它的东西。它可能只是被允许的,没有人质疑它或我的谷歌查询是错误的。
无论如何,基本上我的问题是我可以混合使用不同类型的整数并进行操作吗?
是的你可以。
您的编译器将负责转换。唯一需要担心的是溢出 - 如果您将结果存储在小于输入的容器中。
您需要的 Google 搜索词是“隐式类型转换”——例如参见http://pic.dhe.ibm.com/infocenter/ratdevz/v8r5/index.jsp?topic=%2Fcom.ibm.tpf.toolkit.compilers .doc%2Fref%2Flangref_os390%2Fcbclr21011.htm
该链接包括下表:
算术转换按以下顺序进行:
Operand Type Conversion
---------------------------------------------+--------------------------------------------
One operand has long double type | The other operand is converted to long double type.
---------------------------------------------+--------------------------------------------
One operand has double type | The other operand is converted to double.
---------------------------------------------+--------------------------------------------
One operand has float type | The other operand is converted to float.
---------------------------------------------+--------------------------------------------
One operand has unsigned long long int type | The other operand is converted to unsigned long long int.
---------------------------------------------+--------------------------------------------
One operand has long long int type | The other operand is converted to long long int.
---------------------------------------------+--------------------------------------------
One operand has unsigned long int type | The other operand is converted to unsigned long int.
---------------------------------------------+--------------------------------------------
One operand has unsigned int type |
and the other operand has long int type |
and the value of the unsigned int can be |
represented in a long int | The operand with unsigned int type is converted to long int.
---------------------------------------------+--------------------------------------------
One operand has unsigned int type |
and the other operand has long int type |
and the value of the unsigned int cannot be |
represented in a long int | Both operands are converted to unsigned long int
---------------------------------------------+--------------------------------------------
One operand has long int type | The other operand is converted to long int.
---------------------------------------------+--------------------------------------------
One operand has unsigned int type | The other operand is converted to unsigned int.
---------------------------------------------+--------------------------------------------
Both operands have int type | The result is type int.
---------------------------------------------+--------------------------------------------
C标准说,
如果无符号整数类型的操作数的等级大于或等于另一个操作数类型的等级,则将有符号整数类型的操作数转换为无符号整数类型的操作数的类型。
因此,由于int&unsigned int具有相同的等级,您可以添加它们,并且当您添加它们时,它们int会转换为unsigned int将结果再次保留在 to 中unsigned int。