Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Decimal to Binary in Turing
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
vg19




PostPosted: Fri Jan 23, 2004 11:11 pm   Post subject: Decimal to Binary in Turing

hey,

im kinda new to turing. In school, we are trying to make a deciaml to binary converter. This is what I have right now. (please keep in my im new to turing). Its not wokring properly because it will display numbers higher than 1. Id really apperciate help.

var num1 :int
var binary: int
var temp: int


put "What is the first number?"
get num1
binary := num1 div 256
put binary
temp := (num1-(binary * 256))
binary := temp div 128
put binary
temp := (num1-(binary * 128))
binary := temp div 64
put binary
temp := (num1-(binary * 64))
binary := temp div 32
put binary
temp := (num1-(binary * 32))
binary := temp div 16
put binary
temp := (num1-(binary * 16))
binary := temp div 8
put binary
temp := (num1-(binary * 8))
binary := temp div 4
put binary
temp := (num1-(binary * 4))
binary := temp div 2
put binary
temp := (num1-(binary * 2))
binary := temp div 1
put binary
temp := (num1-(binary * 1))
Sponsor
Sponsor
Sponsor
sponsor
Delos




PostPosted: Sat Jan 24, 2004 10:39 am   Post subject: (No subject)

Binary!

Yeah.

Ok, simple solution. First you need to understand a simple method of decimal-binary conversions:

Take a decimal number, let's take 10.
To convert to binary, it is simply a case of continually dividing by 2 and taking the remainder (using mod). Collect these and you will have the binary.

Let's see:
10/2 = 5 rem 0
05/2 = 2 rem 1
02/2 = 1 rem 0
01/2 = 0 rem 1
exit at 0.

Now, read ^ways, and you get 1010. That is 10 in binary.

Simple eh?

It works a lot more efficiently than hard-coded powers of 2.

Of course for this, you need to know about loops. I'm not sure if you do.
As well, the easiest way I can think of to do this is to store the binary result in a temporary string, then convert it to an int. This is of course kind of complex...but you seem like a really promising programmer! So go for it!

(heheh Very Happy )
shorthair




PostPosted: Sat Jan 24, 2004 11:03 am   Post subject: (No subject)

i suggest putting a limitto the amount of time it divides ,ive had numbers that go one for over 200 divisions , which is just painful
vg19




PostPosted: Sat Jan 24, 2004 11:30 am   Post subject: (No subject)

Hey thanks for the help. I got it to work, but the only probelm is now that the binary number is going in the opposite way. Example decimal#1 is coming out in binary as 10000000.

How can I fix this?

var num1 : int
var binary : real
var temp : real

put "What is the first number?"
get num1
temp := num1 mod 2
put temp..

binary := num1 div 2
temp := binary mod 2
put temp..

binary := binary div 2
temp := binary mod 2
put temp..

binary := binary div 2
temp := binary mod 2
put temp..

binary := binary div 2
temp := binary mod 2
put temp..

binary := binary div 2
temp := binary mod 2
put temp..

binary := binary div 2
temp := binary mod 2
put temp..

binary := binary div 2
temp := binary mod 2
put temp..
Delos




PostPosted: Sat Jan 24, 2004 11:59 am   Post subject: (No subject)

Well...that's no problem. If you do NOT need to manipulate the number, then just do this (I've done this in really basic Turing terms, so it should be simple to understand):

code:

var tempBinaryWord : string := ""

% Get decimal number here...

% Now for every "temp := binary mod 2" type of line do this instead (Don't do the 'put' thing yet):
% I'm hoping you know how to use 'if' structures.
if binary mod 2 = 1 then
tempBinaryWord := tempBinaryWord + "1"
else
tempBinaryWord := tempBinaryWord + "0"
end if


Do this for every div/mod routine.

At the end:

code:

put tempBinaryNumber (8)..
put tempBinaryNumber (7)..
% continue this until you get to:
put tempBinaryNumber (1)


All of this would be much much simpler in more complex constructs, but that can be done later.

For now, this will work!
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 5 Posts ]
Jump to:   


Style:  
Search: