0

Is the following code legal?

struct BigInt {
     uint64_t a : 128;
};

2 Answers2

3

A bitfield must fit within a single int, so you're out of luck unless int is 128 bits on your platform.

(You were also missing a ; at the end of the struct prior to the edit.)

Michael Bullock
  • 976
  • 6
  • 13
  • Bitfield base types `_Bool`, `int` and `unsigned int` are guaranteed. So on an implementation with 128 bit `int`, there will be a 128 bit bitfield. An implementation can provide more types. Considering some support a 128 bit integer on smaller architectures, there might be also a 128 bit bitfield. – too honest for this site Dec 21 '16 at 16:41
0

Not legal in standard C11 (on most platforms; in principle, an int might be 128 bits, but I never met a platform having that). Read n1570 (draft specification).

But on some platforms recent compilers (e.g. GCC 6) provide __int128 as an extension. You won't use that as a bitfield, but as some kind of integral type.

See also this answer to a related question.

Community
  • 1
  • 1
Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • Read the entire standard? How about a snippet? – Fiddling Bits Dec 21 '16 at 16:31
  • 1) The standard does allow 128 bit `int`, so there can be 128 bit bit-fields. 2) The standard allows an implementation to support other types for bit-fields. So `__int128` can be used as bitfield. Conclusion: the standard does not disallow it! (The actual question would be what use it would have). – too honest for this site Dec 21 '16 at 16:43