Back to Recipes Homepage
recipe picture
Recipe: Dez2Bin
1
Your rating: None Average: 1 (1 vote)
Used 25 times with an average impact of 7 points.

Profile

Name: Dez2Bin
ID: 9840
Created on: Mon, 08/30/2010 - 08:20
Updated on: Mon, 08/30/2010 - 15:20
Description:

Converts a float number>=0 to binary and back with limited lua use here.



Best For


Comments

Joined: 09/18/2009
Groups: SETI.Germany
That's how the code does look like

--[[
As the standard library is not included in FoldIt, complex string operations are also not possible,
so generating and manipulating binary numbers by string also doesn't work.
This script uses a table to handle binary numbers instead,
whereas BinTable[1] is the least significant and BinTable[BitAccuracy] is the most significant bit.
SigniPos stores the bit-to-left-shift (end of binary significand, Mantisse in German).
Shifting means, we need to multiply the decimal number as long with 2, until it has got no more digits after decimal point,
before we convert it to a binary number.
Or, in other words, we convert a float value into an integer value, and keep in mind where the decimal point was,
but not on a base of 10 but 2.
If the decimal number has converted, you can use BinTable[1 .. BitAccuracy] to get and modify single bits.
]]--

function Dez2Bin(decimal)

local decimal=decimal
local SigniPos=0

while decimal%1>0 do
SigniPos=SigniPos+1
decimal=decimal*2
end

local BinTable={}

local k
for k=1,BitAccuracy do
local Rest=decimal%2
local Result=(decimal-Rest)/2
decimal=Result
-- print(Result," R ",Rest)
BinTable[k]=Rest

end
return BinTable,SigniPos
end -- function

function Bin2Dez(BinTable,SigniPos)
local BinTable=BinTable
local SigniPos=SigniPos
local Result=0

local k
for k=1,BitAccuracy do
Result=Result+BinTable[k]*2^(k-1)
end -- k

if SigniPos>0 then
local k
for k=1,SigniPos do
Result=Result/2
end -- k
end -- if

return Result
end -- function

-------------------------------------------------------- Script use starts here:

BitAccuracy=64 -- Number of bits, precision

local decimal=0.1234567890123 -- This is the decimal number we want to transform into a binary number
print("Decimal ",decimal," is binary:")
BinTable,SigniPos=Dez2Bin(decimal) -- Execute transformation

OS="" -- Initialize Output string
local k
for k=1,BitAccuracy do -- Fetch bits from LSB to MSB
OS=BinTable[k]..OS -- Append them to string
if k==SigniPos then OS="."..OS end -- If significand starts here, add a point to the string
end
print(OS)

print()
print("Converted back:")
print(Bin2Dez(BinTable,SigniPos)) -- Convert binary number by table and significand information back to decimal number

Want to try?
Add to Cookbook!
To download recipes to your cookbook, you need to have the game client running.
Parent

none

Children

none

Authors
Sitemap

Developed by: UW Center for Game Science, UW Institute for Protein Design, Northeastern University, Vanderbilt University Meiler Lab, UC Davis
Supported by: DARPA, NSF, NIH, HHMI, Amazon, Microsoft, Adobe, RosettaCommons