How would I write a recursive solution in C that does this? For example, if I input 9, it should output 00001001
Asked
Active
Viewed 507 times
-5
-
sorry, typo has been fixed – user5792022 Jan 14 '16 at 22:01
-
Usually when I write code - I sit in front of the keyboard. Pick my nose and scratch my nuts. Have a bash. When I get stuck I post what I have written so far and ask for help – Ed Heal Jan 14 '16 at 22:02
-
Also see http://stackoverflow.com/q/111928/2410359 http://stackoverflow.com/q/32208150/2410359 and many other SO posts – chux - Reinstate Monica Jan 14 '16 at 22:08
1 Answers
1
#include <stdio.h>
#include <stdint.h>
void p(uint8_t n, int times){
if(times){
p(n >> 1, times-1);
putchar("01"[n & 1]);
}
}
void print_bin8(uint8_t num){
p(num, 8);
}
int main(void){
print_bin8(9);
return 0;
}

BLUEPIXY
- 39,699
- 7
- 33
- 70