Created
October 13, 2014 16:26
-
-
Save koturn/4994805b924dd5208e81 to your computer and use it in GitHub Desktop.
Binary literal macro
This file contains hidden or 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> | |
#include <stdlib.h> | |
#define BIN_LITERAL_8(bin) \ | |
( \ | |
((0x0000000f & (0x ## bin)) != 0 ? 1 : 0) | \ | |
((0x0000000f & (0x ## bin >> 4)) != 0 ? (1 << 1) : 0) | \ | |
((0x0000000f & (0x ## bin >> 8)) != 0 ? (1 << 2) : 0) | \ | |
((0x0000000f & (0x ## bin >> 12)) != 0 ? (1 << 3) : 0) | \ | |
((0x0000000f & (0x ## bin >> 16)) != 0 ? (1 << 4) : 0) | \ | |
((0x0000000f & (0x ## bin >> 20)) != 0 ? (1 << 5) : 0) | \ | |
((0x0000000f & (0x ## bin >> 24)) != 0 ? (1 << 6) : 0) | \ | |
((0x0000000f & (0x ## bin >> 28)) != 0 ? (1 << 7) : 0) \ | |
) | |
#define BIN_LITERAL_16(bin1, bin2) \ | |
((BIN_LITERAL_8(bin1) << 8) | BIN_LITERAL_8(bin2)) | |
#define BIN_LITERAL_24(bin1, bin2) \ | |
( \ | |
(BIN_LITERAL_8(bin1) << 16) | \ | |
(BIN_LITERAL_8(bin2) << 8) | \ | |
BIN_LITERAL_8(bin3) \ | |
) | |
#define BIN_LITERAL_32(bin1, bin2, bin3, bin4) \ | |
((BIN_LITERAL_16(bin1, bin2) << 16) | BIN_LITERAL_16(bin3, bin4)) | |
int | |
main(void) | |
{ | |
printf("%d\n", BIN_LITERAL_8(00000001)); | |
printf("%d\n", BIN_LITERAL_8(00000010)); | |
printf("%d\n", BIN_LITERAL_8(00000100)); | |
printf("%d\n", BIN_LITERAL_8(00001111)); | |
printf("%d\n", (short) BIN_LITERAL_16(11111111, 11110000)); | |
printf("%d\n", BIN_LITERAL_32(01111111, 11111111, 11111111, 11111111)); | |
return EXIT_SUCCESS; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment