PDA

View Full Version : Fun With Booleans!



Runaway
09-24-2009, 06:46 AM
Fun With Booleans:
http://img9.imageshack.us/img9/7082/booleanlogic.png (http://img9.imageshack.us/i/booleanlogic.png/)


Most of you probably know what a “Boolean” is, yet do not know exactly what it means or how it receives a true or false result. Boolean logic is the logic of operators, the logic of multiple inputs sending only one output. The average script is full of Boolean logic that most people do not even realize is, in fact, a Boolean. Enough crazy talk, let’s get down to business.


A Boolean has 6 different types of input-output procedures:


And
Or
Nand
Nor
Xor
And Xnor

These all have similar functions, but have varied outputs. Let’s start with AND.


----------------------------------------------------------------------------------------------------------------

AND – One of the most common operators, this requires both inputs to be true in order to output as true.


Input 1 Input 2 Output
-- 0 ------- 0 ------ 0 --
-- 1 ------- 0 ------ 0 --
-- 0 ------- 1 ------ 0 --
-- 1 ------- 1 ------ 1 --

As an example in scar, it would be used like so:

Begin
If FindColor(x, y, 12345, x1, y1, x2, y2) and
FindColor(x, y, 54321, x1, y1, x2, y2) then Exit;
End;
//The script will only exit out of the procedure if, and only if, the script finds both of those colors.


----------------------------------------------------------------------------------------------------------------

OR - Also one of the more common operators, this requires at least one of the inputs to be true in order to output as true.


Input 1 Input 2 Output
-- 0 ------- 0 ------ 0 --
-- 1 ------- 0 ------ 1 --
-- 0 ------- 1 ------ 1 --
-- 1 ------- 1 ------ 1 --

As an example in scar, it would be used like so:

Begin
If FindColor(x, y, 12345, x1, y1, x2, y2) or
FindColor(x, y, 54321, x1, y1, x2, y2) then Exit;
End;
//The script will exit out of the procedure if it finds either one of the specified colors.


----------------------------------------------------------------------------------------------------------------

NAND - Or "Not And", this is the complete opposite of AND. This outputs as true if the inputs are not all true.


Input 1 Input 2 Output
-- 0 ------- 0 ------ 1 --
-- 1 ------- 0 ------ 1 --
-- 0 ------- 1 ------ 1 --
-- 1 ------- 1 ------ 0 --

As an example in scar, it would be used like so:

Begin
If FindColor(x, y, 12345, x1, y1, x2, y2) nand
FindColor(x, y, 54321, x1, y1, x2, y2) then Exit;
End;
//This script will exit out of the procedure if it finds none or one of those colors, but will not exit if it finds both.


----------------------------------------------------------------------------------------------------------------

NOR - Or "Not Or", this is the complete opposite of OR. This outputs as true if both inputs are false.


Input 1 Input 2 Output
-- 0 ------- 0 ------ 1 --
-- 1 ------- 0 ------ 0 --
-- 0 ------- 1 ------ 0 --
-- 1 ------- 1 ------ 0 --

As an example in scar, it would be used like so:

Begin
If FindColor(x, y, 12345, x1, y1, x2, y2) nor
FindColor(x, y, 54321, x1, y1, x2, y2) then Exit;
End;
//This script will exit out of the procedure if, and only if, neither of the specified colors are found.


----------------------------------------------------------------------------------------------------------------

XOR - Or "Exclusive Or", This is a special OR operator. This will output as true if the specified inputs differ from each other.


Input 1 Input 2 Output
-- 0 ------- 0 ------ 0 --
-- 1 ------- 0 ------ 1 --
-- 0 ------- 1 ------ 1 --
-- 1 ------- 1 ------ 0 --

As an example in scar, it would be used like so:

Begin
If FindColor(x, y, 12345, x1, y1, x2, y2) xor
FindColor(x, y, 54321, x1, y1, x2, y2) then Exit;
End;
//This script will exit out of the procedure if it finds one of the colors,
//but will not exit out if it finds neither or both of the colors.


----------------------------------------------------------------------------------------------------------------

XNOR - Or "Not Exclusive Or", This is also a special OR operator. This will output as true if the inputs are both false or both true.


Input 1 Input 2 Output
-- 0 ------- 0 ------ 1 --
-- 1 ------- 0 ------ 0 --
-- 0 ------- 1 ------ 0 --
-- 1 ------- 1 ------ 1 --

As an example in scar, it would be used like so:

Begin
If FindColor(x, y, 12345, x1, y1, x2, y2) xnor
FindColor(x, y, 54321, x1, y1, x2, y2) then Exit;
End;
//This script will exit out of the procedure if it finds neither of those colors, but also if it finds both of them.

----------------------------------------------------------------------------------------------------------------

Those are the 6 different variables that relate to Boolean logic, and can be quite useful when scripting (maybe not XOR :p). I hope you will use some of the more unique Boolean variables now that you have more knowledge of what they do!

Rick
09-24-2009, 07:03 AM
Very nice. I didn't know about NAND.
Nice layout, and I like that Input Output Chart. That helps a lot.

Rich
09-24-2009, 02:16 PM
Very nice, like Richie said. I learned something from it. Nor and Nand. I'd never heard of them until now. Nice going.

Richard.

Runaway
09-25-2009, 05:00 AM
Very nice, like Richie said. I learned something from it. Nor and Nand. I'd never heard of them until now. Nice going.

Richard.


Very nice. I didn't know about NAND.
Nice layout, and I like that Input Output Chart. That helps a lot.

I'm glad I could teach you both something new :)

senrath
09-25-2009, 05:04 AM
Nicely done. Although you missed not and xnor.

Runaway
09-25-2009, 05:09 AM
Nicely done. Although you missed not and xnor.

Well I didn't put not in cause that's just a modifier, but I didn't know about xnor :o

is it for outputting as true when the inputs are either both false or both true?

Like this:
Input 1 Input 2 Output
-- 0 ------- 0 ------ 1 --
-- 1 ------- 0 ------ 0 --
-- 0 ------- 1 ------ 0 --
-- 1 ------- 1 ------ 1 --

:confused:

senrath
09-25-2009, 05:14 AM
Yup. It's simply NOT Exclusively OR.

Runaway
09-25-2009, 05:23 AM
Added. Thanks :p

Zyt3x
09-25-2009, 05:27 AM
Good job :)
I learned something too

senrath
09-25-2009, 05:31 AM
Slight error. Despite meaning "Not Exclusively Or", the operator is XNOR.

Runaway
09-27-2009, 11:22 AM
Slight error. Despite meaning "Not Exclusively Or", the operator is XNOR.

Fixed :p thanks again senrath!

Dgby714
06-11-2011, 07:35 PM
Just a small note


type
Boolean = (False, True);

typedef enum _boolean {
false = 0,
true
} boolean;

Now, If your planing on any C <-> Pascal work, False is 0 and True is non-Zero (Usually 1 or -1)