具有不同int类型的运算 [英] operations with different int types
问题描述
我有一个使用多种不同int类型的程序.
I have a program which uses multiple different int types.
最常用的是uint64_t
和标准int
.但是我想知道我是否可以安全地进行它们之间的混合操作.
Most often used are uint64_t
and the standard int
. However I wonder if I can safely do operations mixed between them.
例如,我有一个uint64_t
,我想向其中添加一个int
,并将该值存储为另一个uint64_t
.
For instance I have an uint64_t
and I want to add an int
to it and store that value as another uint64_t
.
这样做安全吗?在可以对int
进行操作之前,必须将int
强制转换为uint64_t
吗?
Is doing such a thing safe? Do I have to cast the int
to uint64_t
before I can use operations on it?
我真的无法在线上找到有关它的东西.可能只是允许这样做,而没有人质疑它,或者我的Google查询是错误的.
I can`t really find stuff about it online. It might just be allowed and no one questions it or my Google queries are wrong.
无论如何,基本上我的问题是我可以混合使用不同类型的int进行操作吗?
Anyway so basically my question is can I mix and do operations with different types of ints?
推荐答案
是的.
您的编译器将负责转换.唯一需要担心的是溢出-如果将结果存储在小于输入的容器中.
Your compiler will take care of the conversions. The only thing to worry about is overflow - if you store the result in a container that is smaller than the inputs.
The Google search term you need is "implicit type conversions" - see for example http://pic.dhe.ibm.com/infocenter/ratdevz/v8r5/index.jsp?topic=%2Fcom.ibm.tpf.toolkit.compilers.doc%2Fref%2Flangref_os390%2Fcbclr21011.htm
该链接包括下表:
算术转换按以下顺序进行:
Arithmetic conversion proceeds in the following order:
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.
---------------------------------------------+--------------------------------------------
这篇关于具有不同int类型的运算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!