Here's what I got:
enum X {
NONE = 0x00000000,
FLAG_1 = 0x00000001,
FLAG_2 = 0x00000002,
FLAG_3 = 0x00000004,
FLAG_4 = 0x00000008,
FLAG_5 = 0x00000010,
// ...
FLAG_32 = 0x80000000
}
Is there a way to make "bit numbering" automatic so I could like insert a flag so all that goes next get "renumbered"?
I'm just designing an API and I want to keep related flags together, ordered in a specific sequence. The problem is when I add something that goes in the middle I have to manually reassign all numbering that goes after the inserted item. Let's say in my example I want to add FLAG_2A = 0x00000004, and FLAG_3 should be 0x00000008 and so on. Is there a "full auto" way of doing it?
OK, here's the first thing that comes to mind:
#include <stdio.h>
enum { __FLAGS1_BASE = __COUNTER__ };
#define __FLAGS1_CT 1 << (__COUNTER__ - __FLAGS1_BASE - 1)
typedef enum __TEST1 {
FLAG1_0 = 0,
FLAG1_1 = __FLAGS1_CT,
FLAG1_2 = __FLAGS1_CT,
FLAG1_3 = __FLAGS1_CT,
FLAG1_4 = __FLAGS1_CT
} TEST1;
enum { __FLAGS2_BASE = __COUNTER__ };
#define __FLAGS2_CT 1 << (__COUNTER__ - __FLAGS2_BASE - 1)
typedef enum __TEST2 {
FLAG2_0 = 0,
FLAG2_1 = __FLAGS2_CT,
FLAG2_2 = __FLAGS2_CT,
FLAG2_3 = __FLAGS2_CT,
FLAG2_4 = __FLAGS2_CT
} TEST2;
int main() {
printf("X = %u\n", FLAG2_3); // should output 4.
return 0;
}
Is it the only way, or is there something simpler than that?