Skip to content

Instantly share code, notes, and snippets.

@wudi
Created June 18, 2020 04:32
Show Gist options
  • Save wudi/8434cf211cdfcd0ed21f03946aca819e to your computer and use it in GitHub Desktop.
Save wudi/8434cf211cdfcd0ed21f03946aca819e to your computer and use it in GitHub Desktop.
print in binary format
#define SHOW_BITS(a) ({ \
printf("Variable `%s`: ", #a);\
show_bits(&a, sizeof(a));\
})
void show_uchar(unsigned char a)
{
for(int i = 7; i >= 0; i-= 1)
printf("%d", ((a >> i) & 1));
}
void show_bits(void* a, size_t s)
{
unsigned char* p = (unsigned char*) a;
for(int i = s-1; i >= 0 ; i -= 1) {
show_uchar(p[i]);
printf(" ");
}
printf("\n");
}
float float_var = 9.4;
SHOW_BITS(float_var);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment