Skip to content

Instantly share code, notes, and snippets.

@baines
Created June 8, 2019 14:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save baines/773238518c5e76f5c12f8dc11f9664f2 to your computer and use it in GitHub Desktop.
Save baines/773238518c5e76f5c12f8dc11f9664f2 to your computer and use it in GitHub Desktop.
C integer constant variable length string tag macros (up to 8 chars), for enums etc
#include <stdint.h> // uint64_t
#define TSH(a,b) ((uint64_t)(a) << (b * 8))
#define TAG_1(a) (TSH(a,0))
#define TAG_2(b,a) (TSH(b,1)|TSH(a,0))
#define TAG_3(c,b,a) (TSH(c,2)|TSH(b,1)|TSH(a,0))
#define TAG_4(d,c,b,a) (TSH(d,3)|TSH(c,2)|TSH(b,1)|TSH(a,0))
#define TAG_5(e,d,c,b,a) (TSH(e,4)|TSH(d,3)|TSH(c,2)|TSH(b,1)|TSH(a,0))
#define TAG_6(f,e,d,c,b,a) (TSH(f,5)|TSH(e,4)|TSH(d,3)|TSH(c,2)|TSH(b,1)|TSH(a,0))
#define TAG_7(g,f,e,d,c,b,a) (TSH(g,6)|TSH(f,5)|TSH(e,4)|TSH(d,3)|TSH(c,2)|TSH(b,1)|TSH(a,0))
#define TAG_8(h,g,f,e,d,c,b,a) (TSH(h,7)|TSH(g,6)|TSH(f,5)|TSH(e,4)|TSH(d,3)|TSH(c,2)|TSH(b,1)|TSH(a,0))
#define TFN(_1, _2, _3, _4, _5, _6, _7, _8, n, ...) TAG_ ## n
#define TAG(...) TFN(__VA_ARGS__, 8, 7, 6, 5, 4, 3, 2, 1) (__VA_ARGS__)
enum {
TST_ENABLE = TAG('e','n','a','b','l','e'),
TST_DISABLE = TAG('d','i','s','a','b','l','e')
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment