Logic gates are a very useful thing indeed. They can be used for anything from encryption to Graphics programs. Indeed, some RISC processors back in the ol' days relyed mostly on logical gates! Even now, your computer is passing bits through logical gates to get the information you want (using transistors ofcourse). But to tell the truth, you don't really need to know this, I just thought that you might find it handy ... heh heh heh.
Lets start from the start, lets start with the basic concept of a logic gate. A logic gate is alot like a transistor, given two bits it outputs one. I'll start you off with a rather simple gate, the AND gate:
_ | \ BIT1 ------- >| \ | \ |AND |----- > OUT | / BIT2 ------- >| / |_/If you give AND 1 in BIT1 and 1 in BIT2, It will give 1 in OUT
You can clearly see from the above information that AND will always output 0 unless both bits given to it are 1. That's where they get the name really = ). Lets move back a bit and see this in a real life situation.
"If both my desktop computer AND my laptop are on then I'm going to have a big bill to pay"
If neither machines are on (i.e 0 AND 0) then he will not pay a big bill
(0 AND 0 = 0)
If only one of the machines is on (i.e 0 AND 1, 1 AND 0) then he will not pay a big bill (0 AND 1=0, 1 AND 0=0).
If both machines are on (i.e 1 AND 1) then he WILL pay a big bill, the poor guy (1 AND 1=1)
Now, the use of one bit might be trivial but when dealing with computers you can process multiple bits. That's why a computer can process more than one bit at a time, infact as many bits as a register can handel. Here's and example how:
BIT1 1 1 0 1 0 0 1 0 AND AND AND AND AND AND AND AND BIT2 0 1 1 1 1 0 0 0 =========================================================== OUT 0 1 0 1 0 0 0 0here's that up there in code:
mov ax,11010010b and ax,01111000bwhat will remain in ax? 01010000b, ofcourse! Now that we've finished and, it's time to show some more gates.
Right beside AND is it's cousin OR. OR and AND share quite a relationship, but I'll tell you about that later (and if you've already done mathematical redundants, I suggest skiping it =p). OR uses the same concept as AND in this manner:
_ | \ BIT1 ------- >| \ | \ | OR |----- > OUT | / BIT2 ------- >| / |_/If you give OR 1 in BIT1 and 1 in BIT2, It will give 1 in OUT
Puting this into a real life situation:
"If Wafn is on #qbcc or #maths then he's going to get no work done" (hehehe plug = p)
If Wafn is only loged on to #qbcc then he will get no work done. (1 OR 0 = 1)
If Wafn is only loged on to #maths then he will also get no work done. (0 OR 1 = 1)
If Wafn is on both channels then he will still get no work done. (1 OR 1 = 1)
Finally, if Wafn is on neither channels then he will kick back and do some proper work. (0 OR 0 = 0)
Using this concept on a byte like the one above:
BIT1 1 1 0 1 0 0 1 0 OR OR OR OR OR OR OR OR BIT2 0 1 1 1 1 0 0 0 =========================================================== OUT 1 1 1 1 1 0 1 0agian in code this is written like so:
mov ax,11010010b or ax,01111000bAnd, as always, 11111010b is left in ax.
Now for the final two-bit logical gate we are going to discuss here. The eXclusive-OR logical gate is probably one of the most useful of these. While others may not behave like this normally, XOR will return the same byte if it is passed through the gate twice! I'll show you this later but for now the basic concept.
XOR will only return 1 if BIT1 does not equal BIT2.
_ | \ BIT1 ------- >| \ | \ |XOR |----- > OUT | / BIT2 ------- >| / |_/If you give XOR 1 in BIT1 and 1 in BIT2, It will give 0 in OUT
Ok, one last "real" life situation:
"If leroy is bored but not drunk, or if leroy is drunk but not bored nothing interesting will happen in #qbcc"
If leroy was only bored nothing will happen in #qbcc (1 XOR 0=1)
If leroy only had too much to drink nothing will happen in #qbcc (0 XOR 1=1)
If leroy was both drunk and bored (ussally not a good combination =p) then something interesting will happen in #qbcc (1 XOR 1=0)
If leroy was'nt drunk or bored then something interesting will happen in #qbcc (0 XOR 0=1)
This concept applied to the same bytes we used above:
BIT1 1 1 0 1 0 0 1 0 XOR XOR XOR XOR XOR XOR XOR XOR BIT2 0 1 1 1 1 0 0 0 =========================================================== OUT 1 0 1 0 1 0 1 0put into code:
mov ax,11010010b xor ax,01111000bthus, 10101010b is left in ax. But lets not stop there! There is something speacil about this number, let me demostrate something. Lets take what's left in ax, and XOR agian by BIT2.
BIT1 1 0 1 0 1 0 1 0 XOR XOR XOR XOR XOR XOR XOR XOR BIT2 0 1 1 1 1 0 0 0 =========================================================== OUT 1 1 0 1 0 0 1 0hehehehehehe, as you can see passing the product through the original BIT2 gives us back the original BIT1!
so a code like this:
mov ax,11010010b xor ax,01111000b xor ax,01111000bwill leave 11010010b in ax! One of the main uses of this is encryption, but that's not the topic of this file so we'll leave that for later.
Theres one more gate that really does'nt fit into the basics of AND, OR or XOR. That would be the infamous NOT. NOT only requires one BIT, and there are only two conditions that can be attained by this. This digram represents NOT which is alot like a .... diode .... well not really. = p
... Just look at the diagram bewehehehehe:
_ | \ | \ | \ BIT1 ------- >|NOT |----- > OUT | / | / |_/If you give NOT 1 in BIT1, It will give 0 in OUT
All NOT really does is invert a given bit. Here's another "real" life situation:
"If Pasco is loged on don't ask for Pasco, but if Pasco is not loged on Ask where Pasco is."
(this is going to sound as if it's an echo but hey ... NOT is weird =p)
So if Pasco is loged on, then a person wont ask if he's on or not (NOT 1=0)
However, if Pasco is loged off, then a person will ask if he's on (NOT 0=1)
Applying this concept ...
BIT1 1 1 0 1 0 0 1 0 NOT NOT NOT NOT NOT NOT NOT NOT =========================================================== OUT 0 0 1 0 1 1 0 1This can be put into code like so:
mov ax,11010010 not axYou may have realised by now that NOT has the same properities as XOR, and the invert of an invert byte is the original byte. = D
mov ax,11010010 not ax not axWill leave 11010010b in AX. Not as useful in encryption because of it's obviousness =)
Now I promise you, if you skip this bit you wont miss out on a thing. It's just for completeness' sake! Infact, I still don't think there is a use of this in programming, but hey it's pretty damn useful in maths. ; )
________________________ | _____ _____ | | / \/ \ | | | || | | | | || | | | \_____/\_____/ _ _ | | A B A B | |________________________| |____|The above is called a Venn diagram. It's very useful, lets say that A was Byte A and B was Byte B. The intersection of A and B (the part where they meet) is A AND B. The combined area of A and B is A OR B. Now, Not A is the surroding area which does not contain anything of A, similarly NOT B is the surroding area which does not contain anything of B. A useful formula can be gain by using the Venn diagram above.
A or B = A + B - A and Band using algebra:
A and B = A + B - A or BLets see these in action:
lets say A=00110101b and B=00011001b. Using the algorithim before we can tell that A and B equals:
A 0 0 1 1 0 1 0 1 AND AND AND AND AND AND AND AND B 0 0 0 1 1 0 0 1 =========================================================== OUT 0 0 0 1 0 0 0 1thus using binary addition, subtraction A or B = 00110101b + 00011001b - 00010001b
A 0 0 1 1 0 1 0 1 + + + + + + + + B 0 0 0 1 1 0 0 1 =========================================================== A+B 0 1 0 0 1 1 1 0 - - - - - - - - A and B 0 0 0 1 0 0 0 1 =========================================================== A or B 0 0 1 1 1 1 0 1Using the normal algorithim we get the same answer:
A 0 0 1 1 0 1 0 1 OR OR OR OR OR OR OR OR B 0 0 0 1 1 0 0 1 =========================================================== OUT 0 0 1 1 1 1 0 1If you can't figure out how the additions and calculations are done, try converting the binary numbers to decimal and doing it, I'm sure you will attain the same result.
What is the use of this? I have no clue, but it is interesting to know and who knows, you might come up with a use for it (I sure as hell have not). If you do, drop me a line, wont ya? = )
Note: nicks and channels contained in here are for displaying purposes only,
if I have used your nick and you don't want me to use it then tell me, and I
will remove it ... but then why would'nt you want it? = D
This article has been written by abionnnn (firstname.lastname@example.org)