0

Does not need to be portable. As long as it works for one compiler, I can make an ifdef to enable it only on that compiler. I'd prefer a GCC solution but any compiler is fine. I'm using C99.

For context, I'm trying to pack some data together into 32 bits, and one of the fields I'm packing, which I want to represent with an enum, should be only 7 bits wide. If I make it bigger than 7 bits, I'll lose one of the bits, so I'd like a compiler error if I define an enum value that can't fit in 7 bits.

CoffeeTableEspresso
  • 2,614
  • 1
  • 12
  • 30
  • 1
    No way you can encode enum as 7 bits. – Eugene Sh. Jan 24 '20 at 17:47
  • 1
    considered [bit fields](https://www.geeksforgeeks.org/bit-fields-c/) ? – yano Jan 24 '20 at 17:59
  • @yano I don't actually care about the space used to store the enums, I just want to restrict the max value of the enum so that any valid enum value fits in 7 bits. – CoffeeTableEspresso Jan 24 '20 at 18:21
  • I meant use a bit field for the 32 bits you want to pack, then whatever field where the enum goes would be 7 bits. Not sure what you want exactly is possible: https://stackoverflow.com/questions/366017/what-is-the-size-of-an-enum-in-c – yano Jan 24 '20 at 18:25
  • 1
    I know of no way to get a compiler diagnostic if an `enum` entry is above some deliberately set limit. – the busybee Jan 25 '20 at 18:37

0 Answers0