Introduction to Binary Logic Circuits - by Nick Fletcher (2006)


home | page 1 | page 2 | page 3 | page 4 | page 5 | page 6
Let's do some programming now. I want to show you these gates in action. We'll be doing some C++ code, and I must warn you, I am a novice C programmer who doesn't use C++ very much. But, like all good things, practice makes better. The program I am going to write will simulate the four gates (OR, AND, NOT and XOR) we have been looking at. We can then put them to the test in a software version of a real adding machine. (Consider the irony of what we are doing…)
We can then give the adding machine eight bits at a time and let it add 'em together. Go and make a nice drink of something, I'm going to write the code now and de-bug it etc. It may take a while… I'll yell out when I've got it working.



#include <iostream>
using namespace std;

typedef unsigned short int USI;

//class declarations
class OR{
      public:
      OR(){}; //OR's default constructor
      ~OR(){}; //Nothing to release - empty destructor

      USI ShowOutput(USI in1, USI in2);
};

class AND{
      public:
      AND(){}; //AND's default constructor
      ~AND(){};//Nothing to release - empty destructor

      USI ShowOutput(USI in1, USI in2);
};

class NOT{
      public:
      NOT(){};
      ~NOT(){};

      USI ShowOutput(USI in1){
                        return !(in1);
      }
};
int main(int argc, char* argv[]){
      OR       or1;
      AND      and1;

      cout << or1.ShowOutput(0,0) << endl;

      cout << or1.ShowOutput(0,1) << endl;

      cout << or1.ShowOutput(1,0) << endl;

      cout << or1.ShowOutput(1,1) << endl;

      return 0;
}
//class definitions
USI OR::ShowOutput(USI in1, USI in2){
      return in1|in2;
}
USI AND::ShowOutput(USI in1, USI in2){
      return in1 & in2;
}


PROGRAM OUTPUT:
0
1
1
1
This simple program uses C/C++'s BITWISE operators to perform the logic. This version is really just a rudimentary test to see that all is going smoothly. And it seems to be. Just to make sure, I want to expand a bit on the single method of each class. Let's make them more PR savvy.
#include<iostream>
#include<string>
#include<string>
using namespace std;

typedef unsigned short int USI;

//class declarations
class OR{
      public:
      OR(){};
      OR(string nme); // OR's default constructor
      ~OR(){}; //Nothing to release - empty destructor

      USI ShowOutput(USI in1, USI in2);

      private:
      string m_name;
};

class AND{
      public:
      AND(){};
      AND(string nme); // AND's default constructor
      ~AND(){}; // Nothing to release - empty destructor

      USI ShowOutput(USI in1, USI in2);

      private:
      string m_name;
};

class NOT{
      public:
      NOT(){};
      NOT(string name);
      ~NOT(){};

      USI ShowOutput(USI in1){
      cout << endl << this->m_name << " performing NOT on " << in1 << " = ";
      return !(in1);
      }
      private:
      string m_name;
};
//main
int main(int argc, char* argv[]){
      int i;
      cout << "\n\nThe inputs given are: ";
      for(i = 0; i < argc -1; i++){
            cout << atoi(argv[i+1]) << " ";
      }
      cout << endl;
      OR       or1("Or1");
      AND      and1("And1");
      NOT      not1("Not1");

      cout << or1.ShowOutput((USI)atoi(argv[1]),(USI)atoi(argv[2]));

      cout << and1.ShowOutput((USI)atoi(argv[1]),(USI)atoi(argv[2]));

      cout << and1.ShowOutput(or1.ShowOutput((USI)atoi(argv[1]),(USI)atoi(argv[2])),or1.ShowOutput((USI)atoi(argv[1]),(USI)atoi(argv[2])));

      cout << not1.ShowOutput(1);

      cout << not1.ShowOutput(0);

      return 0;
}
//class definitions
OR::OR(string nme){
      m_name = nme;
}
USI OR::ShowOutput(USI in1, USI in2){
      cout << endl << this->m_name << " performing " << in1 << " OR " << in2 << " = ";
      return in1|in2;
}
USI AND::ShowOutput(USI in1, USI in2){
      cout << endl << this->m_name << " performing " << in1 << " AND " << in2 << " = ";
      return in1 & in2;
}
AND::AND(string nme){
      m_name = nme;
}
NOT::NOT(string nme){
      m_name = nme;
}
PROGRAM OUTPUT WHEN CALLED WITH - c:\???>gates2 1 0 <ENTER>

The inputs given are: 1 0

Or1 performing 1 OR 0 = 1
And1 performing 1 AND 0 = 0
Or1 performing 1 OR 0 =
Or1 performing 1 OR 0 =
And1 performing 1 AND 1 = 1
Not1 performing NOT on 1 = 0
Not1 performing NOT on 0 = 1

            
Now the classes provide some info about what they are doing, and I also made provisions to allow us to give the inputs at the command line. This can be handy for rapid evaluation of various parameters. I generalised this so that more than two inputs can be used, incase that's a future requirement. Also notice that I have sent as function parameters an object that's calling one of it's methods. This works because the method returns an appropriate type to use for the parameter. Also note that where those two calls are made, the actual output is not displayed. Do you know why? It's because the output is returned to main() to be displayed. There is nowhere in main() that calls for these two returned values to be displayed, so we have a blank. This does not affect us, so let's just ignore it! This is how I will implement the fourth gate we have studied. The XOR GATE.

home | page 1 | page 2 | page 3 | page 4 | page 5 | page 6
nickeax AT gmail.com