Skip to content

Instantly share code, notes, and snippets.

@minoki
Created March 20, 2016 04:49
Show Gist options
  • Save minoki/49ca1a760ac46dcf67d3 to your computer and use it in GitHub Desktop.
Save minoki/49ca1a760ac46dcf67d3 to your computer and use it in GitHub Desktop.
なんちゃって2進リテラル
#define CONCAT2(x,y) x ## y
#define CONCAT(x,y) CONCAT2(x,y)
#define VAL_END 0)
#define VAL0_END 0) >> 1
#define VAL1_END 0) >> 2
#define VAL2_END 0) >> 3
#define VAL3_END 0) >> 4
#define VAL4_END 0) >> 5
#define VAL5_END 0) >> 6
#define VAL6_END 0) >> 7
#define VAL0(b) (b << 0) + VAL
#define VAL1(b) (b << 1) + VAL0
#define VAL2(b) (b << 2) + VAL1
#define VAL3(b) (b << 3) + VAL2
#define VAL4(b) (b << 4) + VAL3
#define VAL5(b) (b << 5) + VAL4
#define VAL6(b) (b << 6) + VAL5
#define VAL7(b) (b << 7) + VAL6
#define VAL(x) ((CONCAT(VAL7 x,_END))
#define X (1)
#define _ (0)
#if !defined(__cplusplus)
#define static_assert _Static_assert
#endif
static_assert(VAL(X) == 1, "");
static_assert(VAL(_) == 0, "");
static_assert(VAL(_ _) == 0, "");
static_assert(VAL(_ X) == 1, "");
static_assert(VAL(X _) == 2, "");
static_assert(VAL(X X) == 3, "");
static_assert(VAL(_ X _ X) == 5, "");
static_assert(VAL(X _ _ _) == 0x08, "");
static_assert(VAL(X X X X) == 0x0f, "");
static_assert(VAL(_ X _ X X _ X _) == 0x5a, "");
static_assert(VAL(X _ X _ X _ X _) == 0xaa, "");
static_assert(VAL(X X _ X X _ X X) == 0xdb, "");
static_assert(VAL(X X X X X X X X) == 0xff, "");
static_assert(VAL(_ _ _ _ _) == 0x00, "");
static_assert(VAL(X X X X X) == 0x1f, "");
static_assert(VAL(_ X _ X _) == 0x0a, "");
static_assert(VAL(_ X _ X _) == 0x0a, "");
static_assert(VAL(X _ _ X X) == 0x13, "");
static const unsigned char pi[] = {
VAL(_ _ _ _ _),
VAL(X X X X X),
VAL(_ X _ X _),
VAL(_ X _ X _),
VAL(X _ _ X X),
VAL(_ _ _ _ _),
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment