Skip to content

Instantly share code, notes, and snippets.

@gustavorv86
Last active October 3, 2019 06:15
Show Gist options
  • Save gustavorv86/00253f060248b287d6eb407063a02541 to your computer and use it in GitHub Desktop.
Save gustavorv86/00253f060248b287d6eb407063a02541 to your computer and use it in GitHub Desktop.
Print data type as binary format
#include <stdio.h>
#include <stdlib.h>
// MACRO PRINTB
#define PRINTB(x) \
for(int i=(sizeof(x)*8-1) ; i>=0 ; i--) { \
(x & (1 << i)) ? printf("1") : printf("0") ; \
if (i % 4 == 0) { printf(" "); } \
} \
printf("\n"); \
// END MACRO PRINTB
int main() {
char c_dato = 255;
short s_dato = 255;
int i_dato = 255;
PRINTB(c_dato);
PRINTB(s_dato);
PRINTB(i_dato);
getchar();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment