Ok, I must mess with changing decimals to binary and changing binary to decimals etc etc... I can do this using a variable, or using one number. But we have to use a set list of numbers. I don't really know where to begin... I could try to do it all manually but that would result in like a million freekin lines of code.

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?

# decimals to binary

Page 1 of 1## 9 Replies - 862 Views - Last Post: 30 January 2013 - 07:34 PM

##
**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

Are you saying that you know how to change the format manually from decimal to binary format? for example 17 is 00010001. I'm having trouble understanding what assistance you are after.

### #4

## Re: decimals to binary

Posted 29 January 2013 - 10:52 PM

Take a look at using a loop and the bitwise operators '&' and '>>'. If you '&' the number 1 with a number and check its result against 0, you can determine if each digit is a 0 or 1 in the binary. Then append that to a result and continue.

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 pre-pend that answer to "0". Otherwise you call the function again with number / 2 and pre-pend that answer to "1"

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 pre-pend that answer to "0". Otherwise you call the function again with number / 2 and pre-pend that answer to "1"

### #5

## Re: decimals to binary

Posted 29 January 2013 - 10:55 PM

well if using a variable I can do the whole

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.

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

Can anyone explain to me how Convert.ToInt32 works? That seems to be a very easy way to go about converting. But I don't know where it goes in a code nor what libraries are needed.

### #7

## Re: decimals to binary

Posted 30 January 2013 - 07:20 AM

Which flavor of Convert.ToInt32(), there are many variants: http://msdn.microsof...rt.toint32.aspx

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.

Try writing out the code for the first 3-5 digits. You will notice a pattern emerging. This pattern is the core for your loop.

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:

well if using a variable I can do the whole

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.

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 3-5 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:

well if using a variable I can do the whole

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.

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:

Try writing out the code for the first 3-5 digits. You will notice a pattern emerging. This pattern is the core for your loop.

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

As Martyr2 said,

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.

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

Look through the number of bits in the input (this case is 8 for char). For each iteration, shift right by the iteration index. & this result with 1 and print the result. This will give you the binary representation in reverse order, can you think of how you could change the algorithm to reverse it?

This post has been edited by **jjl**: 30 January 2013 - 07:36 PM

Page 1 of 1