Q. Using recursion, write a C++ function into b to convert an arbitrary integer n>=0 into binary. Call this function in the main program with the argument n (That's an easy part I can do that). In example; input n=27, output 11011.

If anyone can help me with this question, I would appriciate that.

7 Years
Discussion Span
Last Post by n1337

Let me try to understand here... The function needs to return a that represents a binary integer? Or the actual bits of the returned value need to be the correct binary representation of the decimal value?


Yes, The function needs to return a that represents a binary integer. And no need to be decimal part. For exmaple; We give n value as 27 to the program, then it gives us an output as (11011)base2. Just base 2 no need another bases.


Oops I made a typo in my previous post. I meant to say "does the function need to return a string that represents the binary integer"? I ask this because the character sequence "10110" is not the same as 10110 (the first has a different bit sequence since each individual character has a corresponding ASCII value, which has its own bit sequence..) For example, the character '1' has ASCII value 0x31..

Anyway, to output the actual bits you can use fprintf( stdout, "%c", __) combined with the bit shift operator >> combined with bitwise & and bitwise |..

But I am guessing that you need the string representation... So you will find repeated mod a % b and division a / b here.. Hint: binary is base 2

Edited by n1337: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.