Skip to main content
guest
Join | Help | Sign In
Liberty BASIC Community Wiki Home
guest| Join | Help | Sign In
Liberty BASIC Community Wiki
  • Wiki Home
  • Recent Changes
  • Pages and Files
  • Members
  • Home
    • Liberty BASIC FAQ
    • Contests
    • Tutorials
    • Science
    • Shared Code
    • Tips
    • DLLs
    • PublicDomainSprites
    • Member Pages
    • Links
    • Sandbox

DecimalToBinary

Edit 0 4 …
  • 0 Tags
    • No tags
  • Notify
  • RSS
  • Backlinks
  • Source
  • Print
  • Export (PDF)
Older Version Newer Version

bshipps64 bshipps64 Feb 18, 2006

DecToBin$ function

Simple function with (very) simple demo program illustrating how to build a string containing the binary representation of a decimal number.

  • Not too much explanation really necessary on this one. The function takes an integer argument and uses a DO...LOOP structure to repeatedly perform a logical AND operation on that argument and each sequential exponentiation of 2 until that exponentiation is greater than the original number. If the function is fed anything other than a nonzero integer value, it returns "0". A small amount of additional coding could also add digit grouping capability.
 [loop] 
input a$
if a$ = "" then end
print DecToBin$(val(a$))
goto [loop]


function DecToBin$(decNum)
if (decNum) then
decNum = abs(decNum)
if (decNum = int(decNum))) then
do
if (decNum and 2^x) then
DecToBin$ = "1" + DecToBin$
else
DecToBin$ = "0" + DecToBin$
end if

x = x + 1
loop until (2^x > decNum)
else
DecToBin$ = "0"
end if
end if
end function
Help · About · Pricing · Privacy · Terms · Support · Upgrade
Contributions to https://basic.wikispaces.com/ are licensed under a Creative Commons Attribution Share-Alike 2.5 License. Creative Commons Attribution Share-Alike 2.5 License
Portions not contributed by visitors are Copyright 2018 Tangient LLC
TES: The largest network of teachers in the world
Turn off "Getting Started"
  1. Home
  2. ...
Loading...