Representing a 64 bit integer in GNU/Linux

11,826

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

Share:
11,826

Related videos on Youtube

smilingbuddha
Author by

smilingbuddha

Updated on July 23, 2022

Comments

  • smilingbuddha
    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) and sizeof(int64_t) are all 8 bytes (64 bits).

    Which qualifier (long, long long, or int64_t) would you recommend for using 64 bit integers?

  • Brett Hale
    Brett Hale over 12 years
    +1. Consider Win64 LLP64 and Unix LP64: en.wikipedia.org/wiki/64-bit#64-bit_data_models
  • sehe
    sehe about 6 years
    DON'T ever put this into your headers. You're going to cause so much pain for your users
  • Robert Andrzejuk
    Robert Andrzejuk about 6 years
    This header can cause this issue : stackoverflow.com/questions/49614169/…

Related