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.

Recommended Answers

All 3 Replies

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.