uint24_t and uint48_t in MinGW

10,534

The standard uintXX_t types are provided in stdint.h (C, C++98) or cstdint (C++11).

On the 8-bit data, 24-bit address AVR architecture, GCC provides a built-in 24-bit integer, but it is not portable. See http://gcc.gnu.org/wiki/avr-gcc for some more info about it.

There is no standard 24-bit or 48-bit integer types provided by GCC or MinGW in a platform independent way, but one simple way to get a portable 24-bit number on just about any platform is to use a bitfield:

struct bitfield24 {
  uint32_t value : 24;
};

bitfield24 a;
a.value = 0xffffff;
a.value += 1;
assert(a == 0);

The same can be done for 48-bits, using a uint64_t as a base.

Share:
10,534
Jamin Grey
Author by

Jamin Grey

Level 27 C++ programmer, and hobbyist indie game developer trying to go professionally indie. Check out my current projects at JaminGrey.com

Updated on June 22, 2022

Comments

  • Jamin Grey
    Jamin Grey almost 2 years

    I'm looking for uint24_t and uint48_t types in GCC and MinGW. I know neither are standardized, but I've come across references to them online, and I'm trying to figure out:

    1. What header I need to include for them.
    2. Whether they are cross-platform (at least on Windows, Linux, and Mac OSX), or just for specific targets.
    3. What their names are. uint24_t, __uint24, __uint24_t?
  • Jamin Grey
    Jamin Grey about 11 years
    Yea, I've heard of that method, though it's much less pleasing. But I google hints in various places where GCC was patched with __uint24 integer types (never came across a 48 bit version). I don't know whether they were unofficial patches or for just specific weird architecture.
  • wjl
    wjl about 11 years
    @Jamin you are right -- it looks like there is a built-in 24-bit type provided in GCC for the AVR architecture. It appears to be mostly for doing address manipulation with special instructions, since the AVR is an 8-bit data, 24-bit address architecture. But if you want anything portable, you're going to have to use something like the technique I posted or by rolling your own integer-like class with operator overloading.
  • Jamin Grey
    Jamin Grey about 11 years
    Thanks, that's unfortunately the conclusion I've come to. Not that I'm opposed to rolling my own classes, but for small native datatypes I just feel like any home-made solution is clunky.