Convert Decimal to Binary Using *Recursion*

Im trying to write code that will take a decimal and convert it to it's 2's complement. Ive been trying to figure it out but i can only get it to working using the functions as void, not string as the requirements have been specified. The only way that has worked so far is through iteration and that is not allowed.

The functions rquired are basically what is being the part that wont allow me to continue.

Function main() calls function string convert(int decimal, int n) that receives the number and returns a string that represents the corresponding binary number in n bits format, where n is the minimum number of bits used to display the binary number.

Function convert() will have to call the *recursive* function string dectobin(int decimal) that receives the decimal number and returns a string that represents the corresponding binary number in free format (that is, with just as many bits as needed to represent the value). Thus, it is the task of convert() to add the (0's and 1's) to the string returned by dectobin() so that the binary number is in n bits format.

The first int in the txt file will be taken in through string convert() as the number of bits, then using that to set the displayed number of bits from the 2's complement representation from function string dectobin().

You should post prototypes of the functions you described verbally above.

Then, make a main function which provides the input arguments (make comments about their meaning) and calls the function(s). Then, make the necessary printouts. With that, you may ask specific questions how to implement the recursive function.

Regards, Alex

P.S. Can you explain the purpose of the second argument of convert function. I didn't get it.

when you implement with proper method, you wont get the values like above. becase the number of bits should be constent (may be 8 multiplies..)
fine,
refer the implementation of itoa function with radix 2. this will definetely help you if you know how to implement recursive function. in this function they may use % operator. since you want to conevert only to binary, you can use shift operators. and before iterating you can find the number of bits for positive numbers, so that you can increase bit performance.

Admin:
I hope i am not voilating EE rules as i have given just some tips. If i am , please let me know.

>> P.S. Can you explain the purpose of the second argument of convert function. I didn't get it.

The minimum number of bits that the result must contain (for 0-padding at the beginning if needed).

>> when you implement with proper method, you wont get the values like above.

Sure you can ... the recursive function can stop at any time, thus returning a string of any length.

@ststoleson : I would suggest to start with the dectobin recursive function. It's not hard ... just let it treat one bit (the % and / operators can help you there), and then call the function recursively ...

Once you have the dectobin recursive function, you can create the convert function, that will call the dectobin function, and pad the result with 0's if needed.

That's it. Give it a try, and post your code here, so we can have a look at it ... and help you where needed.

>>>> I didn't want to confuse him since this is probably one of the first programs he does with C++

Yes, but the bit operators are more appropriate for a task regarding bits than modulo 2 and division by 2. Though the latter may be simpler for a newbee in general, it requires a further abstraction to deduce from that to the bits.

>>>> btw, the generated code (by the compiler) should be pretty much the same.
Hmmm. Do you remember the thread where I assumed that a modulo 11 applied to smaller numbers was faster than applied to bigger numbers? Modulo means division and to turn a division to a bitshift operation means that the compiler actually checks for operand == 2 and make the optimization then. Possible, but I wouldn't bet for it.

We're talking about modulo 2 here ... Any self-respecting modern compiler will realize that it's better to do an & (and in case of division by 2, will realize that a right shift is better), and will generate the exact same instructions in both situations.

division by 11 is slightly more complicated, as there's no way of doing it with one shift. It needs a few instructions, and these might or might not be faster than a single division instruction.

However, you can be pretty sure that a single shift instruction will be faster than a single division instruction, and the compiler knows that, and will thus properly optimize a division by 2 to a right shift.

>> But with the above I can make conversions to decimal string or 'dozen' base as well.

Yes, but then you lose the advantage of the compiler being able to optimize for division by a power of 2.
So, you sacrifice (a bit of) speed for genericity.

Using a templated function would be better if your aim is genericity

@ststoleson : ignore what I just said - it's a discussion between experts, and not relevant for what you have to do ;)

>> i didnt understand, can you tell me how to define differnt number of bits for -ve values.

I don't know what you mean by -ve values, but when you write a recursive function, you can make it stop whenever you want ... In this case, that would be the moment the value becomes 0. That way you only process the number of bits that need to be processed.

>> No we can make it to handle both numbers. But the number of bits will be constant(8bits, 16bits...)

That's not what was required by the exercise (see the example output), nor does it make sense, because for large negative values, the recursive function would waste most of its recursive calls for processing the 0's between the sign bit and the value.

If you're still not convinced, then read this part of the assignment again :

the *recursive* function string dectobin(int decimal) that receives the decimal number and returns a
string that represents the corresponding binary number in free format (that is, with just as many bits
as needed to represent the value). Thus, it is the task of convert() to add the (0's and 1's) to the
string returned by dectobin() so that the binary number is in n bits format.

>>with just as many bits as needed to represent the value
the above statment can be applicaple only for +ve numbers.
lets take two set of size (8bit, 16bit)
for 8 bit:
the value of 7 will be 111
-7 1111 1001

for 16 bit:
the value of 7 will be 111 (same as 8 bit)
-7 1111 1111 1111 1001

so we should decide how many bits output is required for negative values before converting.

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

>> so we should decide how many bits output is required for negative values before converting.

Just read the requirements :

Thus, it is the task of convert() to add the (0's and 1's) to the
string returned by dectobin() so that the binary number is in n bits format.

In other words : convert has one parameter that gives the minimum number of bits. So, bits are prepended if the number of bits returned by dectobin is smaller than this minimum.
Note that the assignment explicitly states that 0's and 1's can be prepended ... Why do you think it mentions 1's ?

>> Why do you think it mentions 1's ?
i dont think the statment says to ommit (leading) 1's. even if says, thats not the correct way to convert.
and can you tell me how do you find how many bits are required for a given -ve number to create a char* memory?

As the requirements say that input is 'int decimal' and 'int n' (for minimum output length) we can deduce 2 things from that:

- it may be passed positive or negative numbers as 'int' is a signed int.
- the number of bits of a 'int', e. g. 32, is one of the parameters we have to consider when dealing with negative numbers.

There is not a single hint in the requirements that 8bit or 16bit boundaries should be used to pad ones to the output string. We don't even have a hint that negative numbers should be printed with their complementary unsigned representation. Though it may be unusual for binary string representation to add a '-' sign at the beginning (e. g. -10011 for decimal -19) I can't see a reason why it is not allowed. If we would do so for any base from 2 ... max bits of int, we are along with the specs and would not mixup with the quite different question how negative numbers were represented in nowadays computer architecture.

>> and can you tell me how do you find how many bits are required for a given -ve number to create a char* memory?

You don't need to - this is C++, and you can use std::string. If you need to allocate memory for the string, then take the maximum amount of bits in an int (usually 32).

>> There is not a single hint in the requirements that 8bit or 16bit boundaries should be used to pad ones to the output string. We don't even have a hint that negative numbers should be printed with their complementary unsigned representation.

Notice the ""'s around "sign bits" - it means that you shouldn't take it literally ... In other words, the base negative value is 1000, and that is extended to 11111000 because the minimum size is 8 bits. In other words, the sign bit is extended.

That's the entire reason for having 2's complement notation : because you can easily extend it, easily add it, etc.

Towards jkr.. This is homework yes however i am not asking anyone to do it for me, if that is the case how will i ever learn myself?

Towards Infinity08.. To answer your question about the experience level i have, id say mabye about 2 semesters, im comfortable with basic functionality and such but advanced concepts is what im having a tough time working out now :-\ , I noticed your very early post and ill start with that approach with starting with the recursive function first and working my way from there..

Towards itsmeandnobodyelse.. thanks for your advice too, ill check into what you have said and try to use that as some help towards my issue.

I guess from now on ill just post code snippets from what i have, and try to see what i can do from there, just feel overwhelmed with the part about keeping it as a string, instead of a void function (dectobin and convert)

alright well here is what i have so far.... i have the functions as bins cause i was having so much problems using the functions as strings :-\ ....

what im trying to do is since i have dectobin converting the int decimal to string binary... i have the function convert calling to it to do so, inside the convert function i have it choose if the number is pos, neg, or 0.

if the 0 then returns 0 to save time, if its negative i have it multiply the decimal by -1 to get a positive number for teh dectobin function to use.

I guess my specific question is now that i have the binary string.. how do i get each specific bit and check if its a 1 or 0 such as for the 2's complement

example if its -6 as the decimal, i have the convert function turn it into +6 then convert it, now what i want to do is use a stack or something of the sort to change to 2's complement so that it still will output as a -6 binary set.

any ideas?

heres my code ive written so far....

(the commented out section of if statements in the dectobin is just a different method of recursion i was trying)

>>>> void dectobin(int num, int base)
The dectobin must return the final string. You also must pass the string so far so that the next recursive call can 'append' a new character.

string dectobin(int num, int base, const string& strnum);

>>>> if( num/base > 0)

Better: simply return the given argument string when num == 0. If you don't use a 'int num' but a 'unsigned int num', the dectobin has not to care for negative integers. That is the task of the 'convert' function (see discussion above).

There's no reason to make the dectobin function work for different bases ... base 2 is all that's required (and the name of the function confirms that). Keep it simple, and once you have it working, you can add new features oif you want. For now, keep it base 2 only, and positive numbers only.

I'll only comment on your dectobin function, because that's the first thing you should get working.

>> i have the functions as bins cause i was having so much problems using the functions as strings :-\ ....

The function has to return a string, because you need to somehow get the result from the function. Returning something from a function happens like this :

int getValue() {
return 5;
}

This function returns an int, and more specifically, the int 5. When you call it like this :

int value = getValue();

then value will be set to 5, because that's what the function returned.

The same is true for strings :

std::string getStr() {
std::string str = "hello";
return str;
}

and called like this :

std::string str = getStr();

will set str to the string "hello", because that was what the function returned.

So, the signature of the dectobin function should not be :

void dectobin(int num, int base);

but :

std::string dectobin(int num);

as in the assignment.

Further, as Alex said : you don't need stringstreams. You can add a character to a string by doing :

ok so i altered dectobin to only handle base 2, and ive gotten it to convert every bit to string, it outputs it correctly but when i return the value of what i am cout'ing inside dectobin, it seems like it is only returning the value of the Least Significant Bit as the value of string dectobin()...

Towards itsmeandnobodyelse: I tried looking into
- static const string all_possible_digits= "0123456789ABCDEF";
but i have no idea how this works, nor could i figured it out, is it static casting? sorry for being a dumb@$$...
what i did find however is a function that will convert it for me, and it works good from what i can see as i tested it by couting during each conversion in the function itself, for when i cout it is in the right order, and it passes the bit each time through the "stringify" function.

So now i have something geared more toward what is being asked of but now im stuck on getting to return the whole string of values instead of just the LSB as it seems its doing now

Towards Infinity08:

I see what you mean about how to add a character to a string, though it adds it on the end so i dont know how you would add it to the front of the string. Like i said earlier, i fixed it so that its simpler and handles only base 2. I know you said to go to what alex said about the static const string but like i said to him, i used that function string stringify().. am i just making this harder on myself or is static string easy to use and im just a dummy?

I was thinking now that i have to convert to string, is there a way i can store y each time its passed through so that itll return the whole enchilada?

>>>> - static const string all_possible_digits= "0123456789ABCDEF";
>>>> but i have no idea how this works, nor could i figured it out,
>>>> is it static casting?

No, a statically defined variable in a function simply was created once and not with each call as it was with non-static variables. The 'all_possible_digits' is just a constant literal (and it doesn't matter whether you make it static or not). You could use it directly in dectobin after you calulated the last digit with modulo:

Awesome Infinity08, thats something towards my final product, i spoke with my professor today and he told me how to turn each bit to char using static casting (this is the way he's prefering it) the function that i have as a final function towards the rest of the project. This is the function as now that returns the string. i am going to build the convert functino now, once im finished ill post the final product for an example for someone else in the future.

Introduction
This article is the first in a series of articles about the C/C++ Visual Studio Express debugger. It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints. Lastly, Part 3 focuses on th…

Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…