Specifying 64-bit unsigned integer literals on 64-bit data models

47,277

Solution 1

You should use <cstdint> / <stdint.h>, if you have it. This will give you:

  • uint64_t is an unsigned integer type which is 64 bits in size
  • UINT64_C() is a macro for creating constants of the type uint64_t, by having the macro append the proper suffix.

Solution 2

For the most part, it doesn't matter. If you don't give it a suffix, the type of an integer literal is determined by its value. If the compiler has a 32-bit unsigned long and a 64-bit unsigned long long, an unsigned value that is too large to fit in an unsigned long but not too large for an unsigned long long will have type unsigned long long.

Solution 3

To my knowledge, there is no way to suffix an integer literal to a specific bit width; your only options are l, ul, ll, and ull.

If you are paranoid about it, you would have to wrap your literals in an #if checking for the sizes of long / long long.

Then again, as KennyTM pointed out above, as long as your literals are in the 64bit range and assigned to a 64bit value, it does not matter much if the literal itself is 64 or 128 bit, or does it?

Solution 4

C and C++ don't have standardized 32/64/128 bit variable types. A long, for example, on some systems is 32 bits and 64 on others. It's annoying, but most OSes do provide some better typedefs to help you with, such as uint32, etc, so you con select an exact type that you need.

This is the job of a good configure script: to determine what the system provides, test that it works, and help you select the right type for the right architecture you're running on.

Share:
47,277
Zach Saw
Author by

Zach Saw

My Work Media Player .NET (MPDN) MPDN Extensions Binaron Serializer QuickPHP SupTitle for AviSynth LANBench Managed C++ for GCC Noteworthy Accomplishments .NET-like framework for native C++ with precise generational mark-and-sweep GC [1] which gives better performance than boost::shared_ptr especially in multithreaded apps. Speed difference is most profound on server boxes with multiple CPUs. I've also created a similar library for GCC - Managed C++ for GCC (MCP). Also found several C# language design oversights [1] causing weird and undesired side effects to programmers, as well as numerous framework bugs [1] [2] [3] (that's to be expected considering the size of the framework). But the biggest of all, finding a critical bug in WOW64. Microsoft OS team stuffed up the WOW64 design of their Windows OS during the design phase of Windows XP x64. They've said it's too costly to change now (design flaws are always the most costly) and would perhaps try to fix it for Windows 8 (we'll see about that). I was surprised that the bug went unnoticed by the brightest people (Boehm GC guys would've seen it for sure) in the world for 11 years before I came along and root caused it in about a day. Feel free to visit my Website. And my Blog.

Updated on July 09, 2022

Comments

  • Zach Saw
    Zach Saw almost 2 years

    As there are various types of 64-bit data models (LLP64/IL32P64, LP64/I32LP64, ILP64, SILP64), what is the standard conforming way of specifying 64-bit unsigned integer literals?

    Would specifying the suffix of ULL be sufficient? Or would I end up causing the literal to be interpreted as 128-bit on some data models?