Representing a 64 bit integer in GNU/Linux
Solution 1
int64_t
-- This is because it is the most portable representation. The other two could be represented differently on other machines.
Solution 2
Do you need exactly 64 bits or at least 64 bits?
Use whichever of int64_t
, int_least64_t
, or int_fast64_t
most clearly expresses your intent. (All three are almost certain to be the same type on current systems, but documenting your intent is valuable.)
All implementations must provide int_least64_t
and int_fast64_t
. It's at least theoretically possible that int64_t
might not exist (say, if the compiler has a 128-bit type but no 64-bit type, or if signed integers aren't represented using 2's-complement).
(But in every C99-ish implementation I've ever seen, long long
is exactly 64 bits, and int64_t
exists.)
Related videos on Youtube
smilingbuddha
Updated on July 23, 2022Comments
-
smilingbuddha almost 2 years
I am using Ubuntu 10.10 (64 bit) with
gcc
and I wanted to use a 64 bit integer in my C++ program.On my system the outputs of
sizeof(long)
,sizeof(long long int)
andsizeof(int64_t)
are all 8 bytes (64 bits).Which qualifier (
long
,long long
, orint64_t
) would you recommend for using 64 bit integers? -
Brett Hale over 12 years+1. Consider Win64 LLP64 and Unix LP64: en.wikipedia.org/wiki/64-bit#64-bit_data_models
-
sehe about 6 yearsDON'T ever put this into your headers. You're going to cause so much pain for your users
-
Robert Andrzejuk about 6 yearsThis header can cause this issue : stackoverflow.com/questions/49614169/…