Can bit-fields be used as a «poor man's» fast integer type?

Viewed 198

I've just noticed an interesting property of gcc with regard to bit-fields. If I create a struct like the following:

template <int N>
struct C
{
    unsigned long long data : N;
};

Then on amd64:

  1. with -m64, for N ∊ <1, 64>, sizeof(C) == 8;
  2. with -m32, for N ∊ <1, 32>, sizeof(C) == 4 and for N ∊ <33, 64>, sizeof(C) == 8.

(with sizeof(unsigned long long) == 8).

That seems to mostly resemble the C99/C++11 uint_fastXX_t except for the fact that on my system sizeof(uint_fast8_t) == 1. But for example, I can't reproduce anything similar with __int128 (which always results in sizeof(C) == 16).

Does it seem like a good idea to you to use the fore-mentioned struct as a «poor man's» replacement for uint_fastXX_t in C++98?

2 Answers
Related