Skip to content

Instantly share code, notes, and snippets.

@aji
Last active December 19, 2015 03:08
Show Gist options
  • Save aji/5887782 to your computer and use it in GitHub Desktop.
Save aji/5887782 to your computer and use it in GitHub Desktop.
Binary constants in C
/* binary.h -- Alex's BIN() macro (and related) */
/* Copyright (C) 2013 Alex Iadicicco */
#ifndef __INC_BINARY_H__
#define __INC_BINARY_H__
#define _BIN_DIGIT(c, n) (!((c) - '1'))
#define _BIN01(x) _BIN_DIGIT((x)[0], 0)
#define _BIN02(x) _BIN01((x)+1) | (_BIN01(x) << 1)
#define _BIN03(x) _BIN02((x)+1) | (_BIN01(x) << 2)
#define _BIN04(x) _BIN03((x)+1) | (_BIN01(x) << 3)
#define _BIN05(x) _BIN04((x)+1) | (_BIN01(x) << 4)
#define _BIN06(x) _BIN05((x)+1) | (_BIN01(x) << 5)
#define _BIN07(x) _BIN06((x)+1) | (_BIN01(x) << 6)
#define _BIN08(x) _BIN07((x)+1) | (_BIN01(x) << 7)
#define _BIN09(x) _BIN08((x)+1) | (_BIN01(x) << 8)
#define _BIN10(x) _BIN09((x)+1) | (_BIN01(x) << 9)
#define _BIN11(x) _BIN10((x)+1) | (_BIN01(x) << 10)
#define _BIN12(x) _BIN11((x)+1) | (_BIN01(x) << 11)
#define _BIN13(x) _BIN12((x)+1) | (_BIN01(x) << 12)
#define _BIN14(x) _BIN13((x)+1) | (_BIN01(x) << 13)
#define _BIN15(x) _BIN14((x)+1) | (_BIN01(x) << 14)
#define _BIN16(x) _BIN15((x)+1) | (_BIN01(x) << 15)
#define _BIN17(x) _BIN16((x)+1) | (_BIN01(x) << 16)
#define _BIN18(x) _BIN17((x)+1) | (_BIN01(x) << 17)
#define _BIN19(x) _BIN18((x)+1) | (_BIN01(x) << 18)
#define _BIN20(x) _BIN19((x)+1) | (_BIN01(x) << 19)
#define _BIN21(x) _BIN20((x)+1) | (_BIN01(x) << 20)
#define _BIN22(x) _BIN21((x)+1) | (_BIN01(x) << 21)
#define _BIN23(x) _BIN22((x)+1) | (_BIN01(x) << 22)
#define _BIN24(x) _BIN23((x)+1) | (_BIN01(x) << 23)
#define _BIN25(x) _BIN24((x)+1) | (_BIN01(x) << 24)
#define _BIN26(x) _BIN25((x)+1) | (_BIN01(x) << 25)
#define _BIN27(x) _BIN26((x)+1) | (_BIN01(x) << 26)
#define _BIN28(x) _BIN27((x)+1) | (_BIN01(x) << 27)
#define _BIN29(x) _BIN28((x)+1) | (_BIN01(x) << 28)
#define _BIN30(x) _BIN29((x)+1) | (_BIN01(x) << 29)
#define _BIN31(x) _BIN30((x)+1) | (_BIN01(x) << 30)
#define _BIN32(x) _BIN31((x)+1) | (_BIN01(x) << 31)
#define _BIN(x) \
((x[ 0] ? (x[ 1] ? (x[ 2] ? (x[ 3] ? \
(x[ 4] ? (x[ 5] ? (x[ 6] ? (x[ 7] ? \
(x[ 8] ? (x[ 9] ? (x[10] ? (x[11] ? \
(x[12] ? (x[13] ? (x[14] ? (x[15] ? \
(x[16] ? (x[17] ? (x[18] ? (x[19] ? \
(x[20] ? (x[21] ? (x[22] ? (x[23] ? \
(x[24] ? (x[25] ? (x[26] ? (x[27] ? \
(x[28] ? (x[29] ? (x[30] ? (x[31] ? \
_BIN32(x) : _BIN31(x)) : _BIN30(x)) : _BIN29(x)) \
: _BIN28(x)) : _BIN27(x)) : _BIN26(x)) : _BIN25(x)) \
: _BIN24(x)) : _BIN23(x)) : _BIN22(x)) : _BIN21(x)) \
: _BIN20(x)) : _BIN19(x)) : _BIN18(x)) : _BIN17(x)) \
: _BIN16(x)) : _BIN15(x)) : _BIN14(x)) : _BIN13(x)) \
: _BIN12(x)) : _BIN11(x)) : _BIN10(x)) : _BIN09(x)) \
: _BIN08(x)) : _BIN07(x)) : _BIN06(x)) : _BIN05(x)) \
: _BIN04(x)) : _BIN03(x)) : _BIN02(x)) : _BIN01(x)) \
: 0 ))
#define BIN(x) _BIN((#x))
/* Use like: BIN(00110111)
Compile with at least -O1, or this becomes horribly inefficient. */
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment