Jump to content




Boolean algebra. with ComputerCraft


  • You cannot reply to this topic
13 replies to this topic

#1 Tjakka5

  • Members
  • 256 posts

Posted 09 May 2014 - 02: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

Edited by Tjakka5, 09 May 2014 - 03:07 PM.


#2 Lignum

  • Members
  • 558 posts

Posted 09 May 2014 - 02: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?

#3 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 09 May 2014 - 02:54 PM

in your mind how is this any different than using or, and, and not?

#4 Tjakka5

  • Members
  • 256 posts

Posted 09 May 2014 - 02: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 by Tjakka5, 09 May 2014 - 02:59 PM.


#5 Lignum

  • Members
  • 558 posts

Posted 09 May 2014 - 03:03 PM

View PostTjakka5, on 09 May 2014 - 02:58 PM, said:

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.

#6 Tjakka5

  • Members
  • 256 posts

Posted 09 May 2014 - 03:09 PM

View PostLignum, on 09 May 2014 - 03:03 PM, said:

View PostTjakka5, on 09 May 2014 - 02:58 PM, said:

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).

#7 viluon

  • Members
  • 183 posts
  • LocationCzech Republic

Posted 09 May 2014 - 03: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 by viluon, 09 May 2014 - 03:19 PM.


#8 Tjakka5

  • Members
  • 256 posts

Posted 09 May 2014 - 03: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.

#9 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 09 May 2014 - 03:28 PM

View Postviluon, on 09 May 2014 - 03:16 PM, said:

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.

View PostTjakka5, on 09 May 2014 - 03:23 PM, said:

I'm simply doing this for fun, and as a learning experience also.
well in that case implement NAND, NOR, XOR, XNOR too :P

#10 Tjakka5

  • Members
  • 256 posts

Posted 09 May 2014 - 03:31 PM

Im currently cracking my head over XOR while my sisters demand I draw bunnies; I'll post some progress later tonight.

#11 viluon

  • Members
  • 183 posts
  • LocationCzech Republic

Posted 09 May 2014 - 03:38 PM

offtopic


#12 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 09 May 2014 - 03:39 PM

View PostTjakka5, on 09 May 2014 - 03:31 PM, said:

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 by theoriginalbit, 09 May 2014 - 03:42 PM.


#13 Tjakka5

  • Members
  • 256 posts

Posted 09 May 2014 - 08: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 by Tjakka5, 09 May 2014 - 08:52 PM.


#14 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 10 May 2014 - 12:57 AM

View PostTjakka5, on 09 May 2014 - 08:51 PM, said:

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 by theoriginalbit, 10 May 2014 - 01:00 AM.






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users