Skip to content

Instantly share code, notes, and snippets.

@bokunodev
Created June 14, 2021 16:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bokunodev/02debc51d8dfc977d4500d39b1018839 to your computer and use it in GitHub Desktop.
Save bokunodev/02debc51d8dfc977d4500d39b1018839 to your computer and use it in GitHub Desktop.
Print decimal binary in C
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
void print_dec_bin(uint64_t d);
int main(int argc, char const *argv[]) {
print_dec_bin(0);
print_dec_bin(1);
print_dec_bin(7);
return 0;
}
void print_dec_bin(uint64_t n) {
int c, k;
printf("%ld: ", n);
for (c = 63; c >= 0; c--) {
k = n >> c;
if (k & 1) {
putc('1', stdout);
} else {
putc('0', stdout);
}
if (!(c % 8)) {
putc(' ', stdout);
}
}
putc('\n', stdout);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment