0

I'm struggling to write a recursive way to print a binary number. Here is what I have so far:

int bin(unsigned char n)
{
    if (n==0) {
        return 0;
    } else {
        printf("%d", bin(n<<1));
    }
}

bin(7)

What seems to be wrong with the logic above?

2
  • With bin(0) it prints nothing. I'd expect a way to print a binary number would produce "0". Commented Feb 25, 2021 at 3:09
  • 4
    Save time and enable all warnings. carl.hiass, Where does int bin() return a value when n==0 is false? Commented Feb 25, 2021 at 3:10

4 Answers 4

1

A few errors such as using << (shift up) instead of >> (shift down). Also not always returning something. I'm surprised the compiler didn't pull you up on that. You don't gain anything from a return value, So you may as well get rid of it.

A simple implementation could look like this. We need to have the wrapped function (bin_recur) to allow us to differentiate between 0 the input value and 0 which signifies its time to stop recursion.

#include <stdio.h>

void bin_recur(unsigned char n)
{
    if (n > 0) 
    {
        printf("%d", n & 1);
        bin_recur(n >> 1);
    }
}

void bin(unsigned char n)
{
    if (n == 0) 
    {
        printf("0\n");
    } else 
    {
        bin_recur(n);
        printf("\n");
    }
}

int main()
{
    for(unsigned i = 0; i < 10; i++)
    {
        bin(i);
    }
}

The fact that your bin function calls printf is not completely ideal. This is what's known as tight coupling and the function could be more reusable if it was not bound to how it is presented. Perhaps copying to a string is a good way or even using fprintf to print to a file.

Sign up to request clarification or add additional context in comments.

Comments

1

Stopping when n is 0 is wrong.

This fails for the trivial case where n is 0 to start with.

What you really need to do is pass down a shift amount as a second argument and stop after 8 bits.

Using the return with value just gets in the way.

Here's some refactored code with a full diagnostic test:

#include <stdio.h>

void
bin2(unsigned char n,int shf)
{

    if (shf <= 7) {
        bin2(n,shf + 1);
        printf("%d", (n >> shf) & 1);
    }
}

void
bin(unsigned char n)
{

    bin2(n,0);
}

int
main(void)
{

    for (unsigned int chr = 0; chr <= 0xFF; ++chr) {
        printf("%2.2X: ", chr);
        bin((unsigned char) chr);
        printf("\n");
    }

    return 0;
}

Comments

0

please add this lines to your code.

  void binary(int n) {
     if(n==0)
        return;

    binary(n/2);
    printf("%d",n%2);
}

2 Comments

or you can add binary(n>>1) for division by 2.
sir language is not a problem.
0

You need the print call to execute after the recursive function for this to work. Here's an example:

void bin(unsigned char n)
{
    if (n > 1) bin(n>>1);
    putchar(n&1 ? '1' : '0');
}

int main(void)
{
    for (unsigned char i = 255; i; i--) {
        printf("%d --> ", i), bin(i);
        getchar(); // inspect element if you want
    }
}

Link to running code here.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.