从 long long 转换为 int 并在 C++ 中反过来

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12031302/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 15:48:26  来源:igfitidea点击:

convert from long long to int and the other way back in c++

c++castingintlong-integer

提问by Loers Antario

How to convert from long long to int and the other way back in c++ ?? also what are the properties of long long , especially its maximum size, thank in advance ..

如何在 C++ 中从 long long 转换为 int 并反过来返回?还有 long long 的属性是什么,特别是它的最大尺寸,提前致谢..

采纳答案by David

Type long long is typically 64 bits.

long long 类型通常为 64 位。

Type int is likely to be 32 bits, but not on all machines.

类型 int 可能是 32 位,但不是在所有机器上。

If you cast an int to a long long, you can do

如果您将 int 转换为 long long,则可以执行

my_long_long = (long long) my_int

and it will be just fine. If you go the other direction, like

一切都会好起来的。如果你去另一个方向,比如

my_int = (int) my_long_long

and the int is smaller than 64-bits, it won't be able to hold all the information, so the result may not be correct.

并且 int 小于 64 位,它将无法保存所有信息,因此结果可能不正确。

回答by Keith Thompson

intis guaranteed to be at least 16 bits wide. On modern systems, it's most commonly 32 bits (even on 64-bit systems).

int保证至少为 16 位宽。在现代系统上,最常见的是 32 位(即使在 64 位系统上)。

long long, which didn't originally exist in C++, is guaranteed to be at least 64 bits wide. It's almost always exactly64 bits wide.

long long,它最初不存在于 C++ 中,保证至少为 64 位宽。它几乎总是正好是64 位宽。

The usual way to convert a value from one integer type to another is simply to assign it. Any necessary conversion will be done implicitly. For example:

将值从一种整数类型转换为另一种类型的常用方法是简单地赋值。任何必要的转换都将隐式完成。例如:

int x = 42;
long long y = 9223372036854775807;
y = x; // implicitly converts from int to long long
x = y; // implicitly converts from long long to int

For a narrowing conversion, where the target type can't represent all the values of the source type, there's a risk of overflow; intmay or may not be able to hold the value 9223372036854775807. In this case, the result is implementation-defined. The most likely behavior is that the high-order bits are discarded; for example, converting 9223372036854775807to intmight yield 2147483647. (This is clearer in hexadecimal; the values are 0x7fffffffffffffffand 0x7fffffff, respectively.)

对于缩小转换,目标类型不能代表源类型的所有值,存在溢出的风险;int可能会也可能不会持有价值9223372036854775807。在这种情况下,结果是implementation-defined。最可能的行为是丢弃高位;例如,转换9223372036854775807int可能会产生2147483647。(这在十六进制中更清楚;值分别是0x7fffffffffffffff0x7fffffff。)

If you need to convert explicitly, you can use a cast. A C-style cast uses the type name in parentheses:

如果需要显式转换,可以使用cast。C 样式转换使用括号中的类型名称:

(long long)x

Or you can use a C++-style static_cast:

或者您可以使用 C++ 样式static_cast

static_cast<long long>(x)

which is somewhat safer than a C-style cast because it's restricted in which types it can operate on.

这比 C 风格的类型转换更安全,因为它限制了它可以操作的类型。

回答by ASHUTOSH

Size of int is only 2 bytes whereas the other one is usually larger than int. So if you are looking to convert long into int then you would end up loosing information. But the other way is possible without sacrificing the correctness of information. Suppose ais of long type and bis of int type. Then int to long covertion:a=(long)b;. For other way:b=(int)a;.

int 的大小只有 2 个字节,而另一个通常大于 int。因此,如果您希望将 long 转换为 int,那么您最终会丢失信息。但另一种方式是可能的,而不会牺牲信息的正确性。假设a是 long 类型并且b是 int 类型。然后 int 到 long 转换:a=(long)b;. 对于其他方式:b=(int)a;