decimals to binary
Page 1 of 19 Replies  661 Views  Last Post: 30 January 2013  07:34 PM
#1
decimals to binary
Posted 29 January 2013  09:45 PM
Would a for loop do this for me? I just don't know how to work this set list of numbers. Since its specific numbers I do not require anything to be entered in this function, therefore I can't scanf() anything in and I just don't know lol.
Could anyone possibly give me a little direction on how to get this rolling?
Replies To: decimals to binary
#2
Re: decimals to binary
Posted 29 January 2013  10:23 PM
#3
Re: decimals to binary
Posted 29 January 2013  10:25 PM
#4
Re: decimals to binary
Posted 29 January 2013  10:52 PM
After that, you can '>>' the number by 1 and do the whole process again. Continue until you reach zero. This is all you need for converting positive decimals to binary. Once you figure out this method going the other direction isn't too bad... check out for looping and pow() to convert back to decimal.
Another way is to use the % (modulus) operator with the number 2 and test that against zero. If it is zero then you can call the function again with the number / 2 (integer division) and prepend that answer to "0". Otherwise you call the function again with number / 2 and prepend that answer to "1"
#5
Re: decimals to binary
Posted 29 January 2013  10:55 PM
a = x%2
z = x/2
b = z%2
and on and on and on and on. But I have a large list of multi digit numbers. It is inconceivable to think to do that for them all. That is what I meant buy manual is to have the program calculate it all out.
#6
Re: decimals to binary
Posted 30 January 2013  01:05 AM
#7
Re: decimals to binary
Posted 30 January 2013  07:20 AM
If you use .NET Reflector, or dotNetPeek, you can look at the source code behind Convert.ToInt32(), but they all work essentially the same way with some optimization behind some of them.
Bawnawgwa, on 30 January 2013  12:55 AM, said:
a = x%2
z = x/2
b = z%2
and on and on and on and on. But I have a large list of multi digit numbers. It is inconceivable to think to do that for them all. That is what I meant buy manual is to have the program calculate it all out.
Try writing out the code for the first 35 digits. You will notice a pattern emerging. This pattern is the core for your loop.
#8
Re: decimals to binary
Posted 30 January 2013  07:33 AM
Bawnawgwa, on 29 January 2013  10:55 PM, said:
a = x%2
z = x/2
b = z%2
and on and on and on and on. But I have a large list of multi digit numbers. It is inconceivable to think to do that for them all. That is what I meant buy manual is to have the program calculate it all out.
You're overstating the problem. You don't care about a "whole list of numbers", because each number should go into a file or array, and be fed into the loop that does this conversion.
for(i=0;i<N;i++) { convert a[i] to binary; }
The assignment is looking for a general number converter, that will work just as well for a single digit, as it does for the greatest int your system can hold (INT_MAX).
The logic is the same. You shouldn't be using code that only works for small numbers, or code that only works for certain int's, but not for others.
That's the whole purpose of the assignment  to show the overall pattern used to convert all ints from decimal to binary and vice versa.
Skydiver said:
So often, like here, that is the case.
You should NOT be using code from somebody else  THINK, and work it out by hand. YOU WILL SEE HOW TO DO IT.
This post has been edited by Adak: 30 January 2013  07:40 AM
#9
Re: decimals to binary
Posted 30 January 2013  01:18 PM
all you really need for this are the '<<' operator and the '&' operator
you need to look up these operators and see what they do,
plus the fact this is not a closed secret but there are many examples of how to
do this in 'c' out there....
It is surprisingly easy to do this and once you have it figured out
well you will be kicking yourself...
Snoopy.
#10
Re: decimals to binary
Posted 30 January 2013  07:34 PM
This post has been edited by jjl: 30 January 2013  07:36 PM
