2

I have an assignment to convert a number from decimal to binary. My problem is that I need to have it printed out directly from the function, instead of returning it back to main. Due to this, my printed out code is in the wrong order. (ex: for 6, my code prints out 011 instead of 110)

This is what I have been using for the function:

int printBinary(int integer) {
    int remainder;

    if (integer < 1) {
        return 0;
    } else {
        remainder = integer % 2;
        printf("%d", remainder);
        integer = integer / 2;
        printBinary(integer);
        return 0;
    }
}

Does anyone have a suggestion on how I can print it out in reverse, or a different approach entirely?

0

3 Answers 3

2

You should just print the digit after the recursive call that prints the higher order bits.

Note also that your function prints nothing for 0, which is probably incorrect.

Here is a simplified version:

int printBinary(int integer) {
    if (integer > 1)
        printBinary(integer / 2);
    printf("%d", integer % 2);
    return 0;
}
Sign up to request clarification or add additional context in comments.

Comments

1

Your function prints binary digits starting from the tail. Just change the order of recursive call and printf.

int printBinary(int integer)  {

    int remainder;
    if (integer < 1)  {
    return 0;
    }
    else  {
        printBinary(integer / 2);
        remainder = integer % 2;
        printf("%d", remainder);
    return 0;
    }
}

It was

printBinary(6);
printf("%d", 0);
printBinary(3);
printf("%d", 1);
printBinary(1);
printf("%d", 1);
printBinary(0);

It is now

printBinary(6);
printBinary(3);
printBinary(1);
printBinary(0);
printf("%d", 1);
printf("%d", 1);
printf("%d", 0);

Comments

0

Also you could try something like this

#include <stdio.h>
#include <limits.h>

void printBinary(int num) {
  int i;

  // findind the first non 0 binary position from MSB
  for (i = sizeof(int) * CHAR_BIT - 1; i >= 0 && (num & (1 << i)) == 0; i--);

  for (; i >= 0; i--) // start the printing from here, so trailing 0's avoided
    if ((num & (1 << i)) == 0)
      printf("0");
    else
      printf("1");
}

int main() 
{
    printBinary(6);
    return 0;
}

4 Comments

i think that the answer is helpful, but the question is about to find conversion using recursive function !
"or a different approach entirely" From this I assumed he may interested in other approach also just to see one at least.
Sorry, your implementation is wrong. I think, it only works correctly from 2 to 15. The first loop seems to be the reason. You are dealing with sizeof(int) which is not the number of bits. I would assume a factor CHAR_BITS from limits.h. for (i = sizeof(int)*CHAR_BIT - 1; i > 0 && (num & (1 << i)) == 0; i--); is better. And if (i != 0) is superfluos.
@MiCo Ohh, you are right. Good catch. Fixed it, thanks.

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.