fork download
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. void print_binary(char* out, unsigned int number, unsigned int size)
  5. {
  6. memset(out, '0', sizeof(char)*size);
  7.  
  8. unsigned int i = 0;
  9. for (unsigned int n = number; n; n = n >> 1) {
  10. if (i < size) {
  11. out[size - i -1] = '0'+(n&1);
  12. }
  13. i++;
  14. }
  15. out[size] = '\0';
  16.  
  17. //putc('!', out);
  18. //printf("%s\n", out);
  19. //return out;
  20. }
  21.  
  22. int main(void) {
  23. char fourtytwo[8];
  24. print_binary(fourtytwo, 42, 8);
  25. printf("%s\n", fourtytwo);
  26.  
  27. char zero[16];
  28. print_binary(zero, 0, 16);
  29. printf("%s\n", zero);
  30.  
  31. char minusfourtytwo[48];
  32. print_binary(minusfourtytwo, -12, 48);
  33. printf("%s\n", minusfourtytwo);
  34. return 0;
  35. }
  36.  
Success #stdin #stdout 0.01s 5288KB
stdin
Standard input is empty
stdout
00101010
0000000000000000
000000000000000011111111111111111111111111110100