Jump to content
 Share

Joshy

Overly Simplified Digital Logic

Recommended Posts

Posted  Edited by Joshy

Programming is a lot of fun and it can make a wonderful career.  I encourage it, but where does it all come from?  Someone might go one level down and suggest you look into an Assembly language, which is an intermediate language between what we can read and what the computer can interpret.  Knowledge in that domain certainly exceeds most needs, but if Assembly translates

 

C = A + B;

 

into the following:

 

add $c, $a, $b
# the $ denotes the "register" location or a memory cell

 

Then what's next?  Let's say you've gone pro or completely insane.  Can you interpret the 0's and 1's?  Certainly!  The above command would be:

 

000000 00001 00010 00011 00000 100000
# We'll pretend that A is in register 00001
# B is in 00010
# C is in 00011

 

You could apply the same understanding to many commands.  Satisfied?  Where do these 0's and 1's come from?  What does it mean to flip a bit?  What are these bits?  You would learn much more about this in your electrical and computer engineering studies, but I'll try my best to scratch the surface here.  You may have heard of the transistors sitting inside of your Central Processing Unit (CPU).  These are your 1's and 0's, but what exactly are they and how do they work?

 

The transistors I'll be talking about are called Metal Oxide Semiconductor Field Effect Transistors (MOSFET).  There are many types such as BJTs, IGBTs, JFETS, and finFETs.  All of this is far beyond the scope of a forum post in a gaming community, and so I'll stick to one because it'll be easier for me to describe.  The MOSFET is a 4 terminal device although one of these terminals is often tied to another one and ignored (the body).  I'll be ignoring it here too.  The other three terminals are the source, gate, and the drain.  The transistor is made up of the semiconductors you may have heard of.  Silicon is a semiconductor, and so that's why they call it the Silicon Valley where companies such as Apple and Microsoft are.  The definition of semiconductor might be clear from the name: It's not a conductor like a metal or wire, but it "kind of" is...  it's "semi-" conductive.  A conductive material allows electricity to flow through it easily.

 

TEjV3Dz.png

 

The wires "source" (S) and "drain" (D) are separated or broken apart with the semiconductor material between it.  The trick to connect them is to make the semiconductor conductive in the region between the two by pulling all of those charges (e-) to the top, and you can do this by applying a voltage to the gate terminal.  It works just like a magnet: Opposites attract.  If you apply a positive voltage to the top (HIGH), then all of the negatively charged e- will want to move there.  When they all gather together to the top, then they connect the source and the drain, and it looks like a conductor.  Now: Current can flow through the source and the drain (it likes to flow from high to low).  You could think of the gate as a water valve controlled by voltage, and the source and the drain as the hose that allows water to flow when the valve is turned.

 

 PbBXzIx.png

 

Circuit designers do not have the time to draw all of the transistors out this way, and so there is a simplified model (above is actually very advanced, but it does come up in interviews from time to time and so I recommend some familiarity if you're interested in the field).  Some people might draw it as a switch that opens and close instead, but I have only see this in power electronics; however: I like that it shows that the transistors is either "on" or "off", which is where your 1's and 0's come from respectively.

 

ANXrzkB.png

 

What can we do now that we have our transistor?  The most classic and basic logic device you could learn about is called an inverter [textbooks will call it a complementary metal oxide semiconductor (CMOS)].  Its name also gives a hint as to its function: It inverts.  High values go inside and low values come out; conversely: Low values go in and high values come out.  The inverter requires two transistors.  We're already familiar with one of these transistors from above; the other is almost the same, but its valve turns the opposite direction according to our water analogy, and it is drawn with a circle in front of it or with an arrow going in the opposite direction.  Whatever turns on the first transistor we knew about turns this one off.  (If you're curious about the names, then the first one you saw is called a NMOS and this new one with a circle is called a PMOS).

 

nsmbU2c.png

 

Let's take a look at an inverter.  How does it work?  How do we flip bits?  Let's try it.

 

n3aSyPo.png

 

[Image on the left] If I put in a HIGH input, then the transistor we knew about (NMOS) turns on; the new one (PMOS) turns off.  So if I set a HIGH voltage on the top and a LOW one on the bottom, then the output will only see the LOW.

[image on the right]  If I put in a LOW input, now the new transistors (PMOS) is willing to turn on, and the old one we learned about (NMOS) turns off.  The output can only see the HIGH one on the top.

 

The simplified drawing for an inverter is the image below:

 

MvLaZc2.png

 

You could reuse this logic for more advanced functions.  You may have heard of an "AND".  ANDs are not so straightforward when it comes to transistors, and so you're more likely to learn about not-AND called a NAND.  not-AND is the inverted values of an AND.  The output of an AND is HIGH only when both of its inputs are HIGH.  If you want an AND, then you'll have to put an inverter behind the NAND.  I'm going to show you the NAND for the sake of time, but this one is only LOW when both inputs are high.  You can try it out for different HIGH and LOW inputs to see for yourself.

 

e23PHbg.png uah4Ylj.png

 

You can kind of see that there are secretly two inverters inside of it tied together in a crafty way.  The simplified drawing for this is below.  Just like our unusual PMOS with the circle in front of it: It's drawn with a circle behind it to show that it's a NAND.  An AND is the same drawing, but without that circle.

 

HQwPv6Y.png

 

A "truth table" for this device would look like below.  You typically line up the inputs on the left side, which I called A and B.  You put the outputs on the right side, and you solve for the output.  Sometimes it might use multiple steps or outputs such as solving for AND and before inverting it into a NAND.

 

a9syaB8.png

 

Your CPU is made up of billions... possibly trillions of transistors as the number in the latest products constantly increase, and so they are composed of many logical devices for your computer to use.  This creates the 1's and 0's the machine language is familiar, which Assembly language can convert into something legible for us; lastly: for programmers to use in their high level languages.  This is definitely overly simplified, but at least you'll have an idea of where it comes from and how it may work a bit.  If you ever wanted to pursue something more, then you might consider a future as an electrical or computer engineer (the sub-field is digital integrated circuits design).

 

Here's what I recommend trying next:

 

  • Try out this not OR one called a NOR.  An OR only requires one HIGH input in order to have a HIGH output, and so the NOR is the inverted version of it and will produce a LOW output.  It's drawn this way:

 

vupT9FO.png

 

and its symbol looks like this:

 

xi3o55X.png

 

  • Try running through some truth tables yourself in C programming:

 

#include<stdio.h>

int main()
{

	int A, B;
	
	printf("Enter input A (1 or 0):\t");
	scanf("%d", &A);
		
	
	printf("Enter input B (1 or 0):\t");
	scanf("%d", &B);
	
	// Change "or" to "and" if you want AND instead.  || and && will work as well.
	printf("A or B is %d", (A or B));
	
	return 0;
	
}

 

  • What happens if you choose something other than 1 or 0?  Try it.
  • Try creating various or more complicated if statements using digital logic instead of if(condition).  You'll notice my code uses bit-wise operations instead of the usual && and ||, which is a very common programming question during interviews.  The hardware implementation of an if statement is called a multiplexer or a MUX if you'd like to read more about it.
#include<stdio.h>

int main()
{

	int UserSelection, power(int base, int exponent);
	int A, B=5, C=123;

	printf("Option 0: A=B\nOption 1: A=C\n");
	printf("Select an option (0 or 1):\t");
	scanf("%d", &UserSelection);
	
	// We'll apply the results to all bits in the cell
	/* If 1 then 11111111 11111111 11111111 11111111  or
	 * If 0 then 00000000 00000000 00000000 00000000 */
	UserSelection=UserSelection*(power(2, 32)-1);
	
	// Equivalent of "if" statement using logic
	A = (B&(~UserSelection))|(C&UserSelection);
	
	printf("A = %d", A);

	return 0;

}

int power(int base, int exponent)
{

	for(int i=0; i<exponent; i++)
	{
	
		base*=base;

	}
	
	return base;
	
}

 

EJzRhnJ.png

 

  • There is more about Conditional Statements that requires using this logic in my other tutorial below.

 

 

Edited by Joshy

PoorWDm.png?width=360&height=152

Share this post


Link to post
Share on other sites


Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now


×
×
  • Create New...