9 Replies - 437 Views - Last Post: 30 January 2013 - 07:34 PM Rate Topic: -----

#1 Bawnawgwa  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 123
  • Joined: 21-January 13

decimals to binary

Posted 29 January 2013 - 09:45 PM

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?

Is This A Good Question/Topic? 0
  • +

Replies To: decimals to binary

#2 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1353
  • View blog
  • Posts: 4,660
  • Joined: 19-February 09

Re: decimals to binary

Posted 29 January 2013 - 10:23 PM

You can initialize an array with a set of numbers.

Arrays
Was This Post Helpful? 0
  • +
  • -

#3 buffalobill  Icon User is offline

  • D.I.C Head

Reputation: 21
  • View blog
  • Posts: 188
  • Joined: 08-July 08

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.
Was This Post Helpful? 0
  • +
  • -

#4 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4354
  • View blog
  • Posts: 12,160
  • Joined: 18-April 07

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"

:)
Was This Post Helpful? 1
  • +
  • -

#5 Bawnawgwa  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 123
  • Joined: 21-January 13

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.
Was This Post Helpful? 0
  • +
  • -

#6 Bawnawgwa  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 123
  • Joined: 21-January 13

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.
Was This Post Helpful? 0
  • +
  • -

#7 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3590
  • View blog
  • Posts: 11,166
  • Joined: 05-May 12

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.

View PostBawnawgwa, 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.


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.
Was This Post Helpful? 0
  • +
  • -

#8 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

Re: decimals to binary

Posted 30 January 2013 - 07:33 AM

View PostBawnawgwa, 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.

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

Was This Post Helpful? 0
  • +
  • -

#9 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 802
  • View blog
  • Posts: 2,363
  • Joined: 20-March 10

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.
Was This Post Helpful? 0
  • +
  • -

#10 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1076
  • View blog
  • Posts: 4,536
  • Joined: 09-June 09

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

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1