Take me home please

The truth on logic gates.

Valid XHTML 1.0!

Valid CSS!

  Traditional symbols

  IEC symbols

  Truth tables

AND gate
Input A Input B Output
low  low  low 
high low  low
low  high low
high high high

AND gate.

OR gate IEC OR gate
Input A Input B Output
low  low  low 
high low  high
low  high high
high high high

OR gate.

EXor gate IEC EXor
Input A Input B Output
low  low  low 
high low  high
low  high high
high high low

EXCLUSIVE OR gate.

legend.

high.binary 1, true, on, positive. 
low.  binary 0, false, off, negative(been near Zero Volts).
X. Don´t care.
Hi-Z. high impedance.

When I did digital electronics during the first year of my radio apprenticeship as a extra course to my normal apprenticeship classes, I had found the old traditional logic symbols very easy to learn, then in later years I became aware of the new IEC (International Electrotechnical Committee) logic symbols. I must say I was confused at first (see the Disclaimer at the bottom of the page) , but then learned the logic behind the characters in all those square boxes, then asked myself,“why was I so confused?”
For the AND gate there is a ampersand “&” all the inputs must be high before the output goes high. (I was never confused on that one)
For the OR gate there is a “more than equals one”, or a “≥1” means one or more input needs to go high before the output goes high.
For the EXCLUSIVE OR gate there is a “equals one” or a “=1” means there can only be one input that can be high for the output to go high, if both gates are high or low then the output is low.

AND gate.

On a basic electrical circuit making a AND gate using just switches, a lamp and power supply, it has just switches in series with a lamp. All the switches must be on for the lamp to go.

IEC OR gate

All right we all might have a list of Integrated Circuits (IC) with AND gate functions in them we like using, but some times you just can not beat old technology and use components that were used before IC were invented. with a couple of signal diodes (like 1N1416) and a pull up resistor you can not only save on component cost but can save space on your Printed Circuit Board (PCB).
When the cathode of one diode goes high (Input A or Input B), the low on the cathode of the other diode will keep the anode (output) low until both cathodes go high.

AND gate using diodes

If you are not so sure on those "truth tables" and what they are trying to tell you , try my little web site toy below, each time you click the "Next State" button the Inputs and Outputs change, with that line been highlighted on the table. Don´t spend two long clicking onto it or you might need to see a doctor, I wonder if I should go and see a .............. oh never mind.

Sometimes the truth table seen to the right might be shown in a format the same as what is on the left

AND gate truth table
Input A Input B Output
0 0 0
1 0 0
0 1 0
1 1 1

 

Now we have a different type of truth table chart, for a two input AND gate.

AND  Input A 
0 1
 Input B  0    
1     red

The last word on the AND gate is the Equation, I must say as a service-person I have never found this very useful, I have done many other useful things both at work and at home that I have learnt from that digital electronics course, but using the gate equations ranks very low. By the way that character in-between the A and B is a dot.

 AND gate Logic Equation 

 A · B = X 

Now you know what the AND gate does (I hope) if you would like to see how the basics of a computer program works have a look at the code below written in C++ for the two lines

 if (input_A==1 && input_B==1)
 std::cout << std::endl << "Output is logic one" << std::endl;

Both input_A and input_B have to "1" (or true is a better word to use in programming) (same as a AND gate function) for the program to print "Output is logic one"
You have to put in the double equals sign otherwise the code with one equals sign will make input_A and or input_B equal to 1 even if it was or should be some other number.

Whether checking a electronic circuit or a computer program a very good short test is as soon as you see one condition low or false then the result will be low or false. Some AND gates can have more than two inputs.

#include <iostream>
using namespace std;

    int input_A;
    int input_B;

int and_gate(void);

int main (void)
{
   int choice;
   std::cout << "Enter 1 to try the AND gate functions " << std::endl ;
   std::cin >> choice;

 if (choice==1)
 {
     and_gate();
 }
      return 0;

}


int and_gate(void)
{

    std::cout << "input Gate A, enter 1 or 0, enter 2 to end program" << "  " ;
    cin >> input_A;
    std::cout << "input Gate B, enter 1 or 0, enter 2 to end program" << "  " ;
    cin >> input_B;
    if (input_A==2 || input==2)
    {
        return 0;
    }
    if (input_A==1&& input_B==1)
         {
         std::cout << std::endl << "Output is logic one" << std::endl;
         }
         else
         {
         std::cout << std::endl <<  "Output is logic zero" << std::endl;
         }
    system("PAUSE");
    and_gate();
}

So why the C++ bit?

You might ask why the C++ when the page starts off with electronic logic gates, I suppose in all honesty I should of used just C since it does not use any of the C++ features, (in that program listed) and the recursion as well, were a function calls a function, well I just don´t think recursion are used enough and this is a useless example to describe the benefit of recursion, so if you are studying C/C++ don´t forget to have a quick look at recursion, who knows one day it might take off, have a nice day ~ someone has too.
Since there are basically three types of gates in electronics and three Operators, not only in programming but also the microprocessor and they work the same way, I thought why not tie them together on this page. In programming they are,

Table of Logical and Bitwise Operators.
AND OR EXCLUSIVE OR
Bitwise Operators. & | ^
Logical Operators. && ||  
Boolean Operators are not used in programming.
Boolean Operators. reddot + orV cirplus

By the way,
Anybody that has learnt C and / or C++ can easy go on to learn Javascript, Java, PHP just to mention three.

OK then lets have the same code written in C.

... do you get the feeling I am making this up as I go along?

#include <iostream>
#include <stdio.h>

    int in_A;
    int in_B;
int and_gate(void);


int and_gate(void)
{
    printf("input Gate A, enter 1 or 0, enter 2 to end program ");
    scanf("%d",&in_A);
    printf("input Gate B, enter 1 or 0, enter 2 to end program ");

    scanf("%d",&in_B);
    if (in_A==2 || in_B==2)
    {
                   return 0;
    }


    if (in_A==1 && in_B==1)
         {
          printf("Output is logic one \n");
         }
         else
         {
          printf("Output is logic zerro \n");
         }
    system("PAUSE");
    and_gate();
}

int main (void)
{
   int choice;
  printf("Enter 1 to try the AND gate functions " );
 scanf("%d",&choice);

 if (choice ==1)
 {
     and_gate();
 }
      return 0;

}

I shouldn´t be so horrible picking on those recursion functions all the time.
Note the the main function is made the last function in C but the first in C++
We still need #include <iostream> for the line system("PAUSE"); to work,
I could of replaced

#include <iostream>
system("PAUSE");

with

char pause;
scanf("%c",&pause);
So the program stops and waits for you to press any key.



Ok so I never realy answered the question "why the C++" or the "C" for that matter, the point is anybody that has studyed logic gate could very well go on to study microprocessors, (like I did) and that is were the "C" comes into it, programing your micro in "C" , you can also program in assembly code, and that suits shorter coding, programing in assembly code also seems to make more efficent use of the program space, I have read programing in assembly code take up about a third less space compared to "C"

OR gate.

For a example of a OR gate using just switches, a lamp and power supply, this has all the switches in parallel with each other and then in series with a lamp. Any one of the switches closed (or turned on) can turn the lamp on.

IEC OR gate

Once again as with the AND gate example above, the OR gate can be made with signal diodes.
When the anode of just one diodes (Input A or Input B) goes high this will make the output go high.

AND gate using diodes
OR Input A
0 1
Input B 0   red
1 red red

Notice there is a plus sign for the OR gate and a dot for the AND gate, brilliant! so easy and considerate to remember, give us a break. Just to confuse the issue, sometimes the letter V is used instead.

 Logic Equation 

 A + B = X 
  How about this!
  If you invert all the inputs and the output, you can change a AND gate into a OR gate and vise versa,
  Also you can swap between a NAND gate and NOR gate, the same way.

  So let´s have look at converting a AND gate into a OR gate, by inverting the inputs and output.
 
  AND gate truth table  
Input A Inverted Input B Inverted Input A Input B Output Output  Inverted
high high low low low high
low high high low low high
high low low high low high
low low high high high low
OR Gate input   OR Gate Output.
 Yep, sure looks like a OR gate to me, even if the OR gate truth table looks upside down to the other ones on this page.

EXOR (EXclusive OR) gate.

Our basic electrical EXOR circuit using switches, lamp and power supply, this has two single pole, double throw switches in series but the Normally Closed (N.C.), Normally Open (N.O.) contacts. Either switches can turn the lamp on or off, great for having the two switches at opposite ends of the house.

EX OR gate
EXOR Input A
0 1
Input B  0    red
 1  red  

You can make a Exclusive OR gate with a OR, NAND and AND gate but I don't have time to tell you more right now. In the mean time if you look at the equation you could make up a different combination.

 Logic Equation 

EXOR equation

The microprocessor.

As you might expect by now the microprocessor would also have the three logical instructions, one each for the AND, OR, Exclusive OR function they are AND, OR and EOR, (not to mention ANDI and ORI, for Load Immediately), they work the same logic as always, so a AND 0b10100101, 0b11000011 doing a bit by bit AND would give a result of 0b10000001.

The thing to note here, when ever the result is zero or 0b00000000 then the "Z" (Zero) flag will the set in the status register.
Also because the most significant bit sets the sign of a binary word (on a signed word), on a eight bit word, when ever the result on bit eight is a one (0b1xxxxxxx) then the "S" (Sign) and "N" (Negative) flags will be set in the status register.

 bit ==> 87 65 43 21
 AND  1st word 10 10 01 01
2nd word 11 00 00 11
 result 10 00 00 01
 
 OR  1st word 10 10 01 01
2nd word 11 00 00 11
 result 11 10 01 11
 
 EOR  1st word 10 10 01 01
2nd word 11 00 00 11
 result 01 10 01 10

The NOT device.

NOT NOT
Input  Output
low  high 
high low

Some people claim this NOT device is a gate, I don´t think so, it just inverts what goes into it. It is not controlled by any other input. On the IEC symbol the square box has a 1 on it, the backward slash on the output tells us the output is inverted.

We can not make a diode (or diodes) do a NOT gate function , we need a transistor and a few resistors, but guess what? there are called Digital transistors out there been used today, I will not tell you what I first thought of when I first saw them named as digital transistors in the data books, who said Integrated Circuits (IC´s) were taking over the world? They are also called Resistor-Equipped Transistors (RET). Whatever you would like to call it, they normally only have either one or two internal resistors connected to the base (input).
Don´t you just love my resistors? I used a W to make them, I suppose you could say they are dubdubdub devices, but then I turned the middle dub upside down to get a more even flow, so I guess it must be a dubmdub device.

NOT PNP gate

NAND gate.

This NAND gate is just the same a AND gate but the output is inverted, and the drawing shows a circle on the output and forward slash on the IEC symbols.

OR gate IEC OR gate
Input A Input B Output
low  low  high 
high low  high
low  high high
high high low

NOR gate.

This NOR gate is just the same a OR gate but the output is inverted, and the drawing shows a circle on the output and forward slash on the IEC symbols.

NOR gate IEC NOR gate
Input A Input B Output
low  low  high 
high low  low
low  high low
high high low

XNOR gate.

This XNOR gate is just the same a EXOR gate but the output is inverted, and the drawing shows a circle on the output and forward slash on the IEC symbols.

XNOR gate IEC XNOR gate
Input A Input B Output
low  low  high 
high low  low
low  high low
high high high

TRI-STATE.

Sometimes you need to connect one or more outputs together which in turn could be connected to one or more inputs, while the one or more inputs is no real problem if you do not exceed the limit, the use of open collector outputs make it possible to connect two or more outputs together, then Tri-state or three state devices came along that does a better job than open collector devices, however a open collector can give you a higher output Volts if you need it.



On the right is my cheap example of a dual tri-state device. The theory should be you do not need the inputs and outputs labeled, the symbols inside the drawing should be enough.

Input Enable Output
X low  Hi-Z
low  high low
high high high

The triangle symbol pointing down next to the output indicates the output are tri-state.

The triangle symbol pointing from left to right indicates signal flow, with the input to the left of the triangle and the output to the right of the triangle.

The "EN" indicates the enable input enables the tri-state device, so when the "Enable input" goes high the outputs no longer appear as high impedance and any logic level on the inputs will appear at the output, as you can see from the tri-state truth table.

So what is a “Low” or a “High”?

So what is a low or high for that matter?
I suppose the best place to get the answer is look at the data sheet, but since this is a tutorial at a web site, I will use a Transistor Transistor Logic (TTL) gate for the example.

  Logic limits for TTL (Vcc supply power is 5 Volts ± 0.5 Volts) 
  Input Output
High VIH High level Input Voltage 2.7 Volts to Vcc
2 Volts (minimum) to Vcc
low  VIL Low level Input Voltage 0 V to 0.5 Volts
0 V to 0.8 Volts (maximum)

The reason why I used TTL in this example over say CMOS is because the power supply to CMOS can very over a wider supply range, But TTL uses more power, and you need lots more bypass capacitors. Notice the big gaps between input and output, high and low this is done to help weed out any noise that might get into the system.

Component handling warning.

Don´t forget when you see this Anti-static sign (or if you see any other warning that the components you are using could be damage by static electricity) to use Anti-static precautions. While you might get away with out using anti-static precautions, sometimes you can partly damage the device, it will still work, but then it could very well fail premature, so the equipment will not last as long as it could do.

 

Acknowledgement.

Many thanks to Kurt who did a bit of prove reading (December 2010) and reported a error most likely dating back to June 2006.

Kurt Mihalik kurt.mihalik*boeing.com
Ku-Band Comm & Track Engineer
Boeing Defense, Space & Security (BDS)
Cape Canaveral, Florida
Wk: (321) 799-6630
Fax: (321) 799-6063

more to come, and tidy up.

Disclaimer: My mind is so fragmented by random excursions into a wilderness of abstractions and incipient ideas that the practical purposes of the moment are often submerged in my consciousness and I often don't know what I'm doing.