I was wondering what would a recursive function that prints all bits of the given variable (can be char
, int
, long
, ...).
Since char allocates 8 bits for a variable, I need to print a total of 8 bits. For example, if the input is 5
, the function should print 00000101
.
I can only come up with iterative function for the same purpose, and it looks like this:
void print_bit_iter(char x, int n)
{
int i, bit;
for(i=n-1; i>=0; i--)
{
bit = (x & (1 << i)) != 0;
printf("%d", bit);
}
}
- The existing question doesn't print preceding zeroes...