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

Boolean algebra. with ComputerCraft

Started by Tjakka5, 09 May 2014 - 01:40 PM
Tjakka5 #1
Posted 09 May 2014 - 03:40 PM
Hey everyone,

I haven't played around with CC in a while, so I decided to do something with it that I have been into for ages;
Boolean Algebra.

You probably know what it is; it's just like redstone, it holds either the state true or false where true overwrites the false.
This is binary, and it's the core of every computer. You use it to make logic gates, so make a ALU, CPU, etc, the possibilies are endless.

So I wrote 2 small snippets of code that simulates the core of binary, the OR and NOT gate.
EDIT: Lignum gave me some better code; thanks

local function OR(a, B)/>/>
  return a or b
end

local function NOT(a)
  return not a
end

The OR gate will look at both inputs, and if either of them is true it will return true.
The NOT gate will return the opposite of the value of the input, so if a is true, it will return false and vice versa.

Using this, and only this, no if statements, no loops, no math. functions we can create everything, and that's what I wanted to share with you guys.


I'll keep this thread updated as I create more things with these 2 basic functions, and my goal will be to have a simple 8 bit CPU.


AND Gate:
Spoiler

local function OR(a, B)/>/>
  return a or b
end

local function NOT(a)
  return not a
end

local function AND(a, B)/>
  return NOT(OR(NOT(a), NOT(B)/>))
end

local a = false
local b = false
local o = AND(a, B)/>/>/>/>
print(o)
Edited on 09 May 2014 - 02:07 PM
Lignum #2
Posted 09 May 2014 - 03:51 PM
You can shorten your functions:

local function OR(a, B)/>
    return a or b
end

local function NOT(a)
    return not a
end

I don't really see the use for this. Can't you just use the built-in operators?
theoriginalbit #3
Posted 09 May 2014 - 03:54 PM
in your mind how is this any different than using or, and, and not?
Tjakka5 #4
Posted 09 May 2014 - 03:58 PM
Because I am a bad programmer when it comes to using those operators; thank for notifying me :)/>

And the things about this is to only be using those 2 functions, not anything like loops or if statements, as that would defeat the whole point.
Edited on 09 May 2014 - 01:59 PM
Lignum #5
Posted 09 May 2014 - 04:03 PM
Because I am a bad programmer when it comes to using those operators; thank for notifying me :)/>

And the things about this is to only be using those 2 functions, not anything like loops or if statements, as that would defeat the whole point.
Alright. But I'm pretty sure that making the equivalent of an if statement, a conditional jump, would be impossible with just these functions.
Tjakka5 #6
Posted 09 May 2014 - 04:09 PM
Because I am a bad programmer when it comes to using those operators; thank for notifying me :)/>

And the things about this is to only be using those 2 functions, not anything like loops or if statements, as that would defeat the whole point.
Alright. But I'm pretty sure that making the equivalent of an if statement, a conditional jump, would be impossible with just these functions.

Yes, but, just to clarify things;

I am using ComputerCraft as a logic simulator. I gave it the 2 core things of binary, OR and NOT, so that you can create every logic gate with it, and eventually a CPU (I hope).
viluon #7
Posted 09 May 2014 - 04:16 PM
Doesn't these function calls slow down the code to a point that it's not effective to use them instead of normal logical operators? (That "CPU" would be simply sooo slow that it'd ruin up any point in using it)
Edited on 09 May 2014 - 02:19 PM
Tjakka5 #8
Posted 09 May 2014 - 04:23 PM
It's also not supposed to be used in "real" programs, I'm simply doing this for fun, and as a learning experience also.
Technically you could eventually run programs on that CPU, but it would be simple programs like NIM I'd reckon, maybe, just maybe Pong.
theoriginalbit #9
Posted 09 May 2014 - 04:28 PM
Doesn't these function calls slow down the code to a point that it's not effective to use them instead of normal logical operators?
yes. well no, the impact would be negligible, but yes, there would be an impact.

I'm simply doing this for fun, and as a learning experience also.
well in that case implement NAND, NOR, XOR, XNOR too :P/>
Tjakka5 #10
Posted 09 May 2014 - 04:31 PM
Im currently cracking my head over XOR while my sisters demand I draw bunnies; I'll post some progress later tonight.
viluon #11
Posted 09 May 2014 - 04:38 PM
offtopic
Im currently cracking my head over XOR while my sisters demand I draw bunnies; I'll post some progress later tonight.
WHAT?!? :DDD
theoriginalbit #12
Posted 09 May 2014 - 04:39 PM
Im currently cracking my head over XOR while my sisters demand I draw bunnies; I'll post some progress later tonight.
you should draw truth tables instead :P/>
XOR is (p ⋁ q) ⋀ ¬(p ⋀ q)
​Since the truth table for XOR would result in
p|q|p⨂q
0|0|0
0|1|1
1|0|1
1|1|0
Edited on 09 May 2014 - 02:42 PM
Tjakka5 #13
Posted 09 May 2014 - 09:51 PM
After much of bunnies, here's what I managed to come up with for the XOR, again, using only ORs and NOTs.

local function XOR(a, B)/>
  return OR(NOT(OR(AND(a, B)/>, NOT(a))), NOT(OR(AND(a, B)/>, NOT(B)/>)))
end

Next up I'll be dealing with half adders and full adders, which I can manipulate to do all the other logic gates using a demux.
Because adders have more variables (8 bit input, 8 bit output, 8 Cin's, 8 Couts) I probably have to setup some variables, unless I can come up with a smart way to… do stuff.
Edited on 09 May 2014 - 07:52 PM
theoriginalbit #14
Posted 10 May 2014 - 01:57 AM
After much of bunnies, here's what I managed to come up with for the XOR, again, using only ORs and NOTs.

local function XOR(a, B)/>/>/>
  return OR(NOT(OR(AND(a, B)/>/>/>, NOT(a))), NOT(OR(AND(a, B)/>/>/>, NOT(B)/>/>/>)))
end
I see two ANDs in there :P/>

just for fun lets make it really not use any ANDs :P/>

local function XOR(p, q)
  return OR(NOT(OR(NOT(OR(NOT(p), NOT(q))), NOT(p))), NOT(OR(NOT(OR(NOT(p), NOT(q))), NOT(q))))
end
and for even more fun, here's the proposition for that
¬((¬(¬p ⋁ ¬q) ⋁ ¬q) ⋁ ¬p) ⋁ ¬((¬(¬p ⋁ ¬q) ⋁ ¬q) ⋁ ¬q)

btw the proposition for your original function is
¬((p ⋀ q) ⋁ ¬p) ⋁ ¬((p ⋀ q) ⋁ ¬q)

both fairly complicated really, especially compared to the proposition I posted to help you with XOR
(p ⋁ q) ⋀ ¬(p ⋀ q)

which would result in

local function XOR(p, q)
  return AND(OR(p,q), NOT(AND(p,q)))
end
much simpler. it does use AND however, but you will want to use AND to be efficient.

EDIT: oh and I really do suggest making your AND this

local function AND(p, q)
  return p and q
end
far more efficient
Edited on 10 May 2014 - 12:00 AM