Computer Science Canada

Please, Need Help To Convert Decimal To Binary Numbers

Author:  Taruen [ Wed May 31, 2006 9:32 pm ]
Post subject:  Please, Need Help To Convert Decimal To Binary Numbers

Hey... i'm lookin for some help in creating a small program that converts decimal or numbers... into binary numbers... any help please.?

Author:  Tony [ Wed May 31, 2006 10:05 pm ]
Post subject: 

000 = 0 = 0
001 = 2^0 = 1
010 = 2^1 = 2
011 = 2^1 + 2^0 = 3
100 = 2^2 = 4
101 = 2^2 + 2^0 = 5
111 = 2^2 + 2^1 + 2^0 = 6

notice any patterns?

Author:  Taruen [ Wed May 31, 2006 10:08 pm ]
Post subject: 

lol yeah i sorta get it.... but i just need help makin a program... like when i click a command button after entering a number in a text box... i want it to show on a label.. the binary number of itl... how would i program that?

Author:  NikG [ Thu Jun 01, 2006 12:49 am ]
Post subject: 

We can't give the code to you, especially considering this is a common assignment in schools from what I remember.

Tony gave you a clue. All you gotta do is find a way to go in the opposite direction that Tony showed.

Author:  Brightguy [ Thu Jun 01, 2006 11:35 am ]
Post subject:  Re: Please, Need Help To Convert Decimal To Binary Numbers

As a hint, you'll want to use Mod and integer division (the \ operator).

Start by finding the least significant bit (the right-most) and then the next least significant, etc. Then get it in a loop so you find one bit each time through the loop.

Author:  NikG [ Thu Jun 01, 2006 11:37 am ]
Post subject: 

I believe I've also managed to successfully do this using string manipulation and a for loop (i.e. no Math operators).

Author:  Tony [ Thu Jun 01, 2006 11:47 am ]
Post subject:  Re: Please, Need Help To Convert Decimal To Binary Numbers

Brightguy wrote:
Start by finding the least significant bit (the right-most) and then the next least significant, etc.

Shouldn't it be the other way around, with the left most?

5 mod 2^2 < 5 -- you've got your first bit (of 3)

Author:  Brightguy [ Thu Jun 01, 2006 4:23 pm ]
Post subject:  Re: Please, Need Help To Convert Decimal To Binary Numbers

Right now I don't see any advantages starting from the most-significant. But if you wanted to do it like that, why even use Mod? Just directly compare to powers of 2.

Author:  Tony [ Thu Jun 01, 2006 4:40 pm ]
Post subject: 

well yeah, pretty much.

How are you even expecting on finding the least significant bit?

Author:  Brightguy [ Thu Jun 01, 2006 6:54 pm ]
Post subject:  Re: Please, Need Help To Convert Decimal To Binary Numbers

Tony wrote:
How are you even expecting on finding the least significant bit?

That's what Mod is for. Even numbers have a 0 for the lsb, odd numbers have a 1. (I'm guessing you already knew that, though...)

I would say there's often a slight advantage doing it from least-to-most significance, since you don't have to start by finding the starting bit.

Author:  Tony [ Thu Jun 01, 2006 7:05 pm ]
Post subject: 

well you'd still need to find the starting bit, otherwise you wouldn't know where to stop Wink

you're right, it could be approached from ether direction. Though when starting with lsb, you'd need to bitshift after determining each bit. As oppose to subtracting 2^n from the decimal value. Main difference being the confusion to the student requiring help with conversions Razz


: