Created
June 14, 2021 16:39
-
-
Save bokunodev/02debc51d8dfc977d4500d39b1018839 to your computer and use it in GitHub Desktop.
Print decimal binary in C
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 <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