I did some google search. Found out you might be able to use __attribute__((packed))
.
http://gcc.gnu.org/onlinedocs/gcc-3.2/gcc/Type-Attributes.html
24 typedef struct __uint48_t uint48_t;
25 struct __attribute__((packed)) __uint48_t {
26 uint64_t _m:48;
27 };
29 void test()
30 {
31 uint48_t a;
32 a._m = 281474976710655;
33 printf("%llu", a._m);
34 printf("%u", sizeof(a));
35
36 a._m = 281474976710656;
37 printf("%llu", a._m);
38 }
main1.c: In function ‘test’:
main1.c:36:2: warning: large integer implicitly truncated to unsigned type [-Woverflow]
$ ./a.out
281474976710655
6
0
But, as you said you are using Windows, which might be different.
I could be wrong, if so just point it out.
BTW, I still do not know what is the best solution to this question.
Using struct makes thing a little awkward (you need call a._m
instead of a
, could we get round with it?) But at least it seems safer than just using uint64_t
.