This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
hobnob11's profile picture

Making A Binary To Decimal Decoder

Started by hobnob11, 16 February 2012 - 11:06 PM
hobnob11 #1
Posted 17 February 2012 - 12:06 AM
Im trying to make a decoder for my binary adder that i made using a wiki page and redpower logic gates, i can make 1-9 but im a bit stuck when it comes to 10…

what i want is some clever piece of code that can instead of having every single number programed in (8bit adder, max is 510…) can know the numbers for 1 - 9 and when a number like 12 is inputted, it can output 1 and 2. also i need to figure out a binary to decimal decoder, wich shouldnt be that hard.

here is what i have so far…


a = colors.white
b = colors.orange
c = colors.magenta
d = colors.lightBlue
e = colors.yellow
f = colors.lime
g = colors.pink
if input == "0 then
	  rs.setBundledOutput(outSide, a + b + c + d + e + f)
elseif input == "1" then
	  rs.setBundledOutput(outSide, e + f)
elseif input == "2" then
	  rs.setBundledOutput(outSide a + b + g + e + d)
....
(i got tierd at this point, you get the idea)

this only works for 1-9 (well this only goes to 2)

basicly i need some sorta magic that says somthing like "1 == rs.setBundledOutput(X, e + f)"

and X can be set to diff sides for diff digits, then somthing like
11= 1, X=outside + 1, X=left

(this is were i got rly confused)

idealy a line of code that can look at the number 11 and see it needs two "1" 's instead of looking through a long list for the number 11, but thats better then doing all of the numbers indvidualy


many thanks in advance
Casper7526 #2
Posted 17 February 2012 - 12:49 AM
Ok… now I just skimmed through your stuff.

To convert from binary to dec you should be able to do.. tonumber("01011101", 2) (pretty sure atleast)

for splitting a number just use string.sub or string.gmatch
hobnob11 #3
Posted 17 February 2012 - 10:14 AM
im a complete noob at this, but im sorta getting it. i can do the binary to decimal, because the diffreent bits have diff colours, i just need to do somthing like

colors.white x 1 = bit1
colors.orange x 2 = bit2
colors.magenta x 4 =bit3
colors.lightBlue x 8 = bit4…

then bit1 + bit2 + bit3 + bit4 == bits



so then i would do somthing like errr, dont know how to put it. string.sub(bits) == digit1, digit2, digit3

then errr

if digit1 == "0" then
rs.setBundledOutput(outSide, a + b + c + d + e + f)
elseif digit1 == "1" then
rs.setBundledOutput(outSide, e + f)
elseif digit1 == "2" then
rs.setBundledOutput(outSide a + b + g + e + d)

and then the same for digit 2 and 3, can someone help me put this together…
Casper7526 #4
Posted 17 February 2012 - 08:04 PM


function tobit(p)
  return 2 ^ (p - 1)
end
function hasbit(x, p)
  return x % (p + p) >= p	  
end

function setbit(x, p)
  return hasbit(x, p) and x or x + p
end

function clearbit(x, p)
  return hasbit(x, p) and x - p or x
end
myoutput = 0
myoutput = setbit(myoutput, tobit(1)) -- white
myoutput = setbit(myoutput, tobit(2)) -- orange
myoutput = setbit(myoutput, tobit(3)) -- magenta
myoutput = setbit(myoutput, tobit(4)) -- lightblue
-- the colors go in order as you see them 
-- white,orange,magenta,lightBlue,yellow,lime,pink,gray,lightGray,cyan,purple,blue,brown,green,red,black
print (hasbit(myoutput, tobit(4)))
print (hasbit(myoutput, tobit(5)))
myoutput = clearbit(myoutput, tobit(4))
print (hasbit(myoutput, tobit(4)))