#164654 - 2006-07-19 11:14 PM
Decimal to Binary converter
|
Muesli
Fresh Scripter
Registered: 2006-07-17
Posts: 26
|
I made a litle converter, so decimal numbers can be changed into binary. I made a program for this once on a TI83+ for an exam. And thought it would be fun to do in Kix. Please tell me what you think of it.
Code:
Break ON
:Start $X=1 $Power=1 $Length=1 $Binval="" $Exponent=2 $Expval=1 CLS ?" BitCrusher" ?" Decimal to Binary Calculator" ?" Made by Muesli" ???" Decimal value: " Gets $Decval $EndDecval= $Decval While $X=1 While Val($Expval) < Val($Power) $Exponent= Val($Exponent) *2 $Expval= Val($Expval) +1 Loop If Val($Decval) - Val($Exponent)<1 $X=2 Goto "Result" Else $Power= Val($Power) + 1 $Length= Val($Length) + 1 EndIf Loop :Result
While $X=2 $Exponent=2 If $Length<0 $X=3 Goto 'END' EndIf While Val($Expval) > 0 $Exponent= Val($Exponent) *2 $Expval= Val($Expval) -1 Loop If Val($Decval) - Val($Exponent)>=0 $Decval= Val($Decval) - Val($Exponent) $Binval= $Binval + "1" Else $Binval= $Binval + "0" EndIf $Power= Val($Power) - 1 $Length= Val($Length) - 1 $Expval= Val($Power) Loop :END If $Decval=1 $Binval= $Binval + "1" Else $Binval= $Binval + "0" EndIf
??"The Decimal number "$EndDecval" In Binary is "$Binval ? ?"Again? y/n" Get $Key If $Key=y $X=1 Goto "Start" Else Exit EndIf
|
|
Top
|
|
|
|
#164656 - 2006-07-20 03:30 AM
Re: Decimal to Binary converter
|
Shawn
Administrator
   
Registered: 1999-08-13
Posts: 8611
|
TI99/4A for me - loved that box - got me hooked on programming.
|
|
Top
|
|
|
|
#164657 - 2006-07-20 04:06 AM
Re: Decimal to Binary converter
|
Les
KiX Master
   
Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
|
My first PC came as a kit. http://www.zebrasystems.com/zebrasystems/zx81/parts.htm
Muesli, I might suggest that when indenting, you indent further than just one level to better show nesting. I notice you did not indent the first of the nested While Loop.
|
|
Top
|
|
|
|
#164658 - 2006-07-20 03:27 PM
Re: Decimal to Binary converter
|
Muesli
Fresh Scripter
Registered: 2006-07-17
Posts: 26
|
Thanx for the tips, I went and put the indenting in and took out the "GOTOs" It does look a lot neater like this. Code:
Break ON $A=1 $Z=1
While $A=1
If $Z=1 $X=1 $Power=1 $Length=1 $Binval="" $Exponent=2 $Expval=1 $Z=0 CLS ?" BitCrusher" ?" Decimal to Binary Calculator" ?" Made by Muesli" ???" Decimal value: " Gets $Decval $EndDecval= $Decval EndIf While $X=1 While Val($Expval) < Val($Power) $Exponent= Val($Exponent) *2 $Expval= Val($Expval) +1 Loop If Val($Decval) - Val($Exponent)<1 $X=2 Else $Power= Val($Power) + 1 $Length= Val($Length) + 1 EndIf Loop While $X=2 $Exponent=2 While Val($Expval) > 0 $Exponent= Val($Exponent) *2 $Expval= Val($Expval) -1 Loop If Val($Decval) - Val($Exponent)>=0 $Decval= Val($Decval) - Val($Exponent) $Binval= $Binval + "1" Else $Binval= $Binval + "0" EndIf $Power= Val($Power) - 1 $Length= Val($Length) - 1 $Expval= Val($Power) If $Length<0 $X=3 EndIf Loop If $X=3 If $Decval=1 $Binval= $Binval + "1" Else $Binval= $Binval + "0" EndIf ??"The Decimal number "$EndDecval" In Binary is "$Binval ? ?"Again? y/n" Get $Key If $Key=y $X=1 $Z=1 Else Exit EndIf EndIf
Loop
|
|
Top
|
|
|
|
#164659 - 2006-07-21 12:59 AM
Re: Decimal to Binary converter
|
kholm
Korg Regular
   
Registered: 2000-06-19
Posts: 714
Loc: Randers, Denmark
|
Hi Muesli
Welcome
Your code works fine Since maciep is busy playing 'golf', I will give an example using division instead of multiplication. I'm sure you haven't had the MOD operator available on Your TI83 but dig in and enjoy the strength of KiX
Code:
Break On While 1 CLS ?" BitCrusher" ?" Decimal to Binary Calculator" ?" Made by ???" ???" Decimal value: " GetS $DecVal $DecVal = 0 + $DecVal $EndDecVal= $DecVal $BinVal = '' If $DecVal < 2 $BinVal = $DecVal Else While $DecVal > 1 $Bit = $DecVal Mod 2 If $Bit $BinVal = '1' + $BinVal Else $BinVal = '0' + $BinVal EndIf $DecVal = ($DecVal - $Bit) / 2 Loop $BinVal = '1' + $BinVal EndIf ??"The Decimal number " + $EndDecVal + " In Binary is " + $Binval ? ?"Again? y/n" Get $Key If $Key<>y Exit EndIf Loop
-Erik
ps. The smallest code is not always the fastest
|
|
Top
|
|
|
|
#164660 - 2006-07-21 08:54 AM
Re: Decimal to Binary converter
|
Richard H.
Administrator
   
Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
|
The current golf challenge includes a base converter, so I can guarantee that once the private phase is over you will be seeing some very small, very fast alternatives
|
|
Top
|
|
|
|
#164661 - 2006-07-21 01:33 PM
Re: Decimal to Binary converter
|
maciep
Korg Regular
   
Registered: 2002-06-14
Posts: 947
Loc: Pittsburgh
|
yeah, that's why i haven't posted my variation yet
|
|
Top
|
|
|
|
#164662 - 2006-07-22 10:52 PM
Re: Decimal to Binary converter
|
maciep
Korg Regular
   
Registered: 2002-06-14
Posts: 947
Loc: Pittsburgh
|
Code:
dim $decVal,$binVal,$temp,$key do CLS ?" BitCrusher" ?" Decimal to Binary Calculator" ???" Decimal value: " Gets $decVal $temp = 0+$decVal $binVal='' do $binVal = '' + $temp mod 2 + $binVal $temp = $temp/2 until $temp=0
??"The Decimal number " $decVal " In Binary is " $binVal ? ?"Again? (y/n) " Gets $key until $key = 'n'
|
|
Top
|
|
|
|
#164663 - 2006-07-23 03:59 AM
Re: Decimal to Binary converter
|
NTDOC
Administrator
   
Registered: 2000-07-28
Posts: 11634
Loc: Space
|
That's great Eric, how about a REAL UDF for it since you've gone this far
|
|
Top
|
|
|
|
#164664 - 2006-07-24 03:18 PM
Re: Decimal to Binary converter
|
DrillSergeant
MM club member
   
Registered: 2004-07-09
Posts: 1164
Loc: Eijsden, the Netherlands
|
Code:
Function b($) dim $a
If $+0=$ Do $b = iif($ mod 2,'1','0') + $b $ = $ / 2 Until $ = 0 Else $a = 1 While $ $b = iif(val(right($,1)), $a, 0) + $b $a = $a * 2 $ = Left($,~) EndFunction
Kix score = 137
|
|
Top
|
|
|
|
#164666 - 2006-07-31 11:16 PM
Re: Decimal to Binary converter
|
Lonkero
KiX Master Guru
   
Registered: 2001-06-05
Posts: 22346
Loc: OK
|
137? so, let's golf it a bit. 107 sounds a lot better: Code:
Function b($) do If $+0=$ $b = iif($ mod 2,'1','0') + $b $ = $ / 2 else $b = 2*$b + left($,1) $ = right($,~) endif Until $ = . EndFunction
|
|
Top
|
|
|
|
#164667 - 2006-08-01 12:09 AM
Re: Decimal to Binary converter
|
Lonkero
KiX Master Guru
   
Registered: 2001-06-05
Posts: 22346
Loc: OK
|
thought that we should go under 100, so here it is, 99: Code:
Function b($) do If $+0=$ $b = '' + ($ mod 2) + $b $ = $ / 2 else $b = 2*$b + left($,1) $ = right($,~) endif Until $ = . EndFunction
|
|
Top
|
|
|
|
#164668 - 2006-08-01 12:16 AM
Re: Decimal to Binary converter
|
Lonkero
KiX Master Guru
   
Registered: 2001-06-05
Posts: 22346
Loc: OK
|
mod is a oddball thingie, but anyways, this seems to work fine too: Code:
Function b($) do If $+0=$ $b = '' + $ mod 2 + $b $ = $ / 2 else $b = 2*$b + left($,1) $ = right($,~) endif Until $ = . EndFunction
and the score is now 97
|
|
Top
|
|
|
|
#164671 - 2006-08-01 12:51 PM
Re: Decimal to Binary converter
|
Muesli
Fresh Scripter
Registered: 2006-07-17
Posts: 26
|
This is embarrising  My original code with 837 Characters broken down to 97  Got some learning to do thanks to that
|
|
Top
|
|
|
|
#164672 - 2006-08-01 01:48 PM
Re: Decimal to Binary converter
|
DrillSergeant
MM club member
   
Registered: 2004-07-09
Posts: 1164
Loc: Eijsden, the Netherlands
|
Quote:
just a side note... notice that the shortest code is not missing any endif's or loop's one could say, it's syntaxwise perfect code.
pfft... 
Code:
Function b($) While $ If $+0=$ $b = '' + $ mod 2 + $b $ = $ / 2 Else $b = 2*$b + left($,1) $ = right($,~) EndFunction
Score = 88
|
|
Top
|
|
|
|
Moderator: Glenn Barnas, NTDOC, Arend_, Jochen, Radimus, Allen, ShaneEP, Ruud van Velsen, Mart
|
0 registered
and 809 anonymous users online.
|
|
|