 Computer Science Canada Programming C, C++, Java, PHP, Ruby, Turing, VB   Username:   Password: Wiki Blog Search Turing Chat Room Members
Decimal to Binary in Turing       Author Message
vg19 Posted: 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))    Delos  Posted: 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 ) shorthair  Posted: 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 Posted: 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  Posted: 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: All Posts1 Day7 Days2 Weeks1 Month3 Months6 Months1 Year Oldest FirstNewest First        Page 1 of 1  [ 5 Posts ]
 Jump to:  Select a forum  CompSci.ca ------------ - Network News - General Discussion     General Forums   -----------------   - Hello World   - Featured Poll   - Contests     Contest Forums   -----------------   - DWITE   - [FP] Contest 2006/2008   - [FP] 2005/2006 Archive   - [FP] 2004/2005 Archive   - Off Topic     Lounges   ---------   - User Lounge   - VIP Lounge     Programming -------------- - General Programming     General Programming Forums   --------------------------------   - Functional Programming   - Logical Programming   - C     C   --   - C Help   - C Tutorials   - C Submissions   - C++     C++   ----   - C++ Help   - C++ Tutorials   - C++ Submissions   - Java     Java   -----   - Java Help   - Java Tutorials   - Java Submissions   - Ruby     Ruby   -----   - Ruby Help   - Ruby Tutorials   - Ruby Submissions   - Turing     Turing   --------   - Turing Help   - Turing Tutorials   - Turing Submissions   - PHP     PHP   ----   - PHP Help   - PHP Tutorials   - PHP Submissions   - Python     Python   --------   - Python Help   - Python Tutorials   - Python Submissions   - Visual Basic and Other Basics     VB   ---   - Visual Basic Help   - Visual Basic Tutorials   - Visual Basic Submissions     Education ----------- - Student Life   Graphics and Design ----------------------- - Web Design     Web Design Forums   ---------------------   - (X)HTML Help   - (X)HTML Tutorials   - Flash MX Help   - Flash MX Tutorials   - Graphics     Graphics Forums   ------------------   - Photoshop Tutorials   - The Showroom   - 2D Graphics   - 3D Graphics     Teams ------ - dTeam Public

 Style: Appalachia blueSilver eMJay subAppalachia subBlue subCanvas subEmjay subGrey subSilver subVereor Search: