8

I need to represent a 40-bit binary number. Which C datatype should be used to handle this?

Kobi
  • 1,395
  • 4
  • 17
  • 23

4 Answers4

10

If you're using a C99 or C11 compliant compiler, then use int_least64_t for maximum compatibility. Or, if you want an unsigned type, uint_least64_t. These are both defined in <stdint.h>

<stdint.h> usually also defines int64_t, but since it's not required by the standard, it may not be defined in every implementation. However:

  • int_least64_t - at least 64 bits, and
  • int_fast64_t - the fastest size in this implementation of at least 64 bits

are both required to be present in C99 and C11 (See § 7.18.1.2-3 in the C99 standard, and § 7.20.1.2-3 in the C11 standard).


Although C99 specifies that long long is at least 64 bits on a particular machine (§ 5.2.4.2.1), the types in <stdint.h> are designed to be explicitly portable.

You can read more about integer sizes on different platforms here. Note that the size of integer types are a problem with the long data type - on 64 bit Windows, it's currently 32 bits, whereas on 64 bit linux it's 64 bits. For this reason, I believe you're safest using the types from <stdint.h>

It's worth noting that some feel that long long is more readable. Personally, I prefer the types from <stdint.h>, because they allow you to say what you mean when you use them - which I find more readable. Of course, readability is often a matter of taste - and if you're working with an existing codebase, I'd just follow whatever they do :)


If your compiler only supports C89, then R..'s solution will allow you up to 53 bits of integer precision.

Community
  • 1
  • 1
Timothy Jones
  • 21,495
  • 6
  • 60
  • 90
  • 2
    I disagree. `long long` is in fact theoretically *more* portable, because a conforming C99 or C11 implementation must provide that type whereas `int64_t` is not necessarily present. `int64_t` should be used only if you require an exactly 64 bit two's complement type with no padding - in cases like this, where you just need "at least 40 bits" then `long long` is preferable, since it does not add unncessary restrictions. – caf Mar 07 '12 at 04:57
  • @caf: Doesn't C99 also specify `int64_t` and family? (And as far as I know, they're still present in C11). If you don't want to specify exactly 64 bit width, then you could use `int_least64_t` or `int_fast64_t`. – Timothy Jones Mar 07 '12 at 05:09
  • @Timothy: They're optional; `int64_t` cannot exist unless unless `char` is 8-bit, and also might not exist for various other reasons. `int_least64_t` is required to exist and I suppose it's more "correct" (and less likely to be wasting space) than `long long`, but it sure is ugly... – R.. GitHub STOP HELPING ICE Mar 07 '12 at 05:19
  • @TimothyJones: C99 specifies them as optional (if an implementation doesn't have a type meeting the requirements, it doesn't have to provide `int64_t` - this is of course unlikely at the present time as a practical matter, which is why I said "theoretically"). `int_least64_t` is better, because it is required. However I contend that using a smattering of `int64_t`, `int_least64_t` and `int_fast64_t` is likely to make your functions less composable than if you just used the base types. – caf Mar 07 '12 at 05:21
  • @caf @R You're right. I've updated the answer to better reflect the standard. – Timothy Jones Mar 07 '12 at 05:32
3

long long is an integer type with at least 64 bits. It will fit signed 40 bit values on all platforms. You can also use unsigned long long for unsigned values of 64 bits or more.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
3

You can use long long, which can contain 64 bits or more. For more information, see

http://en.cppreference.com/w/cpp/language/types

http://en.wikipedia.org/wiki/Integer_(computer_science)#Common_long_integer_sizes

Define the 64 bit width integer in Linux

Community
  • 1
  • 1
Adam Liss
  • 47,594
  • 12
  • 108
  • 150
1

The existing answers are good, but if you want C89 support too, double will be able to store 40-bit integers exactly on all IEEE 754 conformant implementations.

R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711