uint24_t and uint48_t in MinGW

2020-03-03 04:59发布

问题:

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?

回答1:

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.



标签: c++ gcc mingw