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

118,989

Solution 1

Type long long is typically 64 bits.

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

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

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.

Solution 2

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

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

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; int may 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 9223372036854775807 to int might yield 2147483647. (This is clearer in hexadecimal; the values are 0x7fffffffffffffff and 0x7fffffff, respectively.)

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

(long long)x

Or you can use a C++-style 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.

Solution 3

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 a is of long type and b is of int type. Then int to long covertion:a=(long)b; . For other way:b=(int)a;.

Share:
118,989

Related videos on Youtube

Loers Antario
Author by

Loers Antario

Updated on January 23, 2020

Comments

  • Loers Antario
    Loers Antario over 4 years

    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 ..

  • adu
    adu over 10 years
    Does it just truncate the higher order bits when casting to a smaller size?
  • Keith Thompson
    Keith Thompson over 9 years
    @adu: Maybe. That's the typical behavior, but the result is implementation-defined for a signed target type. For an unsigned target type, the result is defined by the language, and is equivalent to discarding the high-order bits.
  • Keith Thompson
    Keith Thompson over 9 years
    The width of int is at least 16 bits. It's typically 32 bits on modern systems. (sizeof (int) could be as small as 1, but only if CHAR_BIT >= 16, which is unusual.)
  • FourtyTwo
    FourtyTwo over 6 years
    Downvoting this, since I believe a C++ cast like static_cast should always be preferred over C-style-casts when coding C++ .
  • Guinzoo
    Guinzoo over 6 years
    @FourtyTwo static_cast is too verbose and for that simple digital to digital conversion c-cast looks better, imho.
  • FourtyTwo
    FourtyTwo over 6 years
    @Guinzoo This is what most programmers think when they start using C++. But please reconsider: Somebody might refactor the code to actually make my_int a pointer instead of a local variable (the variable name might not reflect this). Then, guess what: The cast still succeeds, but gives you the address instead of the value of the variable. With static_cast, you get a compiler error. There are many ways of creating undefined behavior or even crashes of an application like this! Let the compiler help you where it can - the new C++ casts are there to help, and are not verbose at all!
  • Keith Thompson
    Keith Thompson over 6 years
    The casts aren't necessary. You can just assign the values: my_long_long = my_int; or my_int = my_long_long; and the conversion will be done implicitly. This is actually safer, since there's no risk of using the wrong type in the cast.