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

Help with Code! Binary Addition

Started by gsholbert, 02 June 2012 - 03:16 AM
gsholbert #1
Posted 02 June 2012 - 05:16 AM
So I have a program I've been trying to work on the past few days. It takes a bundled input and does binary addition for me. The easiest way I could think to program it would be through a ripple-carry type procedure. I was planning on using variables updated by wire states. I tried to write a program that threw an error that I could not find. Now I've thrown that one out and started over, and it still does not work with what I have so far. Lua is a new language to me. please help. Any help will be credited.

Specifications
  • Needs to have variables, updated by wire state
  • Needs to accept two bundled inputs
  • Needs to change variables under scenarios: 0, 1, 2, and 3
  • prints output
  • Outputs to bundled cable
Variables needed:
  1. x and x' for different inputs, respectively (maybe hex values?)
  2. S0, S1, S2, etc. (sum of each place)
  3. C1, C2, etc. (Carry, second number is what place to carry to)
  4. Possibly for outputs
the first addition should be a+a'
second C1+b+b'
etc



Thank you in advance for any help or suggestions
D3matt #2
Posted 02 June 2012 - 06:01 AM
Isn't there a binary API?
Luanub #3
Posted 02 June 2012 - 06:04 AM
Isn't there a binary API?

Nope, you're probably thinking of the bit API
MysticT #4
Posted 02 June 2012 - 10:47 PM
rs.getBundledInput returns a number, wich is the combination of colors that are on in the cable. The colors are represented by bits in the number, so you can just add them like any other number. You can use the bit.band function to get the first bits in case of an overflow, so you can output it to the cable.
Example:

local function addInput(i1, i2)
  local result = i1 + i2
  if result > 65535 then -- check for overflow
	result = bit.band(result, 65535)
  end
  return result
end
Just pass the values you get from rs.getBundledInput and it should work (tested). Like this:

local i1 = rs.getBundledInput("left") -- get first input
local i2 = rs.getBundledInput("right") -- get second input
local result = addInput(i1, i2) -- add them with the previous function
print(i1, " + ", i2, " = ", result) -- print the result
rs.setBundledOutput("back", result) -- set the result on the output cable
kazagistar #5
Posted 03 June 2012 - 02:24 AM
If you need to rearrange which bit corresponds to which color, that can be done using a map.

map = {}
map[colors.red]=1
map[colors.green]=2
map[colors.orange]=4
map[colors.purple]= 8
...
map[colors.blue]=32768
-- Make sure you have an entry for each color, or you might get errors

function map.convert(input)
  current = bundled_rs_input
  sum = 0
  i=1
  while current>0 do
    if current%2 ~= 0 then
	  sum = sum + map[i]
    end
    current=current/2
    i = i*2
  end
  return sum
end

print(map.convert(rs.getBundledInput("back")))

Note, this is untested, so it might have a syntax error or two
gsholbert #6
Posted 03 June 2012 - 10:10 PM
Thank you guys so much. I didn't know about most of these functions. What about place though? with a 16 bit number, is there multiple ways to write one number?
gsholbert #7
Posted 03 June 2012 - 10:38 PM
Now I'm getting an "attempt to call nil" error. I've looked but I can't find why.
MysticT #8
Posted 03 June 2012 - 11:40 PM
Thank you guys so much. I didn't know about most of these functions. What about place though? with a 16 bit number, is there multiple ways to write one number?
Binary numbers can't be written in different ways, like numbers in any base.

Now I'm getting an "attempt to call nil" error. I've looked but I can't find why.
Post the code so we can see the error.
gsholbert #9
Posted 04 June 2012 - 02:41 AM
I'm assuming that you meant for me to add something to the code, but like I said, I'm very new at lua, and 16.



local i1 = rs.getBundledInput("left") -- get first input
local i2 = rs.getBundledInput("right") -- get second input
local result = addInput(i1, i2) -- add them with the previous function
print(i1, " + ", i2, " = ", result) -- print the result
rs.setBundledOutput("back", result) -- set the result on the output cable

local function addInput(i1, i2)
  local result = i1 + i2
  if result > 65535 then -- check for overflow
	    result = bit.band(result, 65535)
  end
  return result
end
gsholbert #10
Posted 04 June 2012 - 05:03 AM
Nevermind. The problem was the addInput. I don't know why, but when I changed it to " local result = i1+i2 " it worked. Will this still allow me to use the bit.band?

-edit-
I feel like a noob *facepalm* once the addInput function was in front of the variable declarations, it worked like a charm. Thank you guys for your help :)/>/>
gsholbert #11
Posted 04 June 2012 - 05:07 AM
If I loop this, it would continually print. How would I loop it while only printing on redstone update?

-edit-
nevermind. sleep will work
MysticT #12
Posted 04 June 2012 - 08:03 PM
You can wait for "redstone" events, and then update:

local side1 = "left"
local side2 = "right"
local outputSide = "back"

local function addInput(i1, i2)
  local result = i1 + i2
  if result > 65535 then -- check for overflow
	result = bit.band(result, 65535)
  end
  return result
end

while true do
  os.pullEvent("redstone")
  local i1 = rs.getBundledInput(side1)
  local i2 = rs.getBundledInput(side2)
  local result = addInput(i1, i2)
  print(i1, " + ", i2, " = ", result)
  rs.setBundledOutput(outputSide, result)
end