-
-
Save tekknolagi/d4aecf683cc4d2655d0c0cdfbcae428e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#define IS_DIG(x) (((x)|1)=='1') | |
#define SLEN(s) ((sizeof s)-1) | |
#define RIDX(s,x) ((x)<SLEN(s)?s[SLEN(s)-1-(x)]:0) | |
#define CNT(s,x,n) ((x)<n?IS_DIG(RIDX(s,(x))):0) | |
#define CNT4(s,x,n) (CNT(s,x+0,n)+CNT(s,x+1,n)+CNT(s,x+2,n)+CNT(s,x+3,n)) | |
#define CNT16(s,x,n) (CNT4(s,x+0,n)+CNT4(s,x+4,n)+CNT4(s,x+8,n)+CNT4(s,x+12,n)) | |
#define CNT64(s,x,n) (CNT16(s,x+0,n)+CNT16(s,x+16,n)+CNT16(s,x+32,n)+CNT16(s,x+48,n)) | |
#define VAL(s,x) ((RIDX(s,x)=='1')<<(CNT64(s,0,x))) | |
#define VAL4(s,x) (VAL(s,x+0)+VAL(s,x+1)+VAL(s,x+2)+VAL(s,x+3)) | |
#define VAL16(s,x) (VAL4(s,x+0)+VAL4(s,x+4)+VAL4(s,x+8)+VAL4(s,x+12)) | |
#define VAL64(s,x) (VAL16(s,x+0)+VAL16(s,x+16)+VAL16(s,x+32)+VAL16(s,x+48)) | |
#define PARSE_BIN_STRING(s) VAL64(#s,0) | |
int main() | |
{ | |
printf("%u\n", PARSE_BIN_STRING(0b1_1011)); // 27 | |
printf("%u\n", PARSE_BIN_STRING(0100_0001)); // 65 | |
printf("%u\n", PARSE_BIN_STRING(1 0 0 1)); // 9 | |
printf("%u\n", PARSE_BIN_STRING(this 1s s0 c001 )); // 17 | |
printf("%u\n", PARSE_BIN_STRING(10101010101010101010101010101010)); // 2863311530 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment