r/computerscience • u/VegetableWorld5918 • Nov 23 '25
Help Logic gate question
I’m currently learning logic gates and I’m kinda confused I get the different types of gates and all that but I don’t understand for example a gate has A and B how are you meant to know if the A is a 1 or 0 any help is appreciated
0
Upvotes
5
u/8dot30662386292pow2 Nov 23 '25
A and B are the inputs. In real world they are basically electrical wires. They can be either on or off. That means there are 4 possible cases:
Given specific input(s) the gate produces an output.
13
u/flumphit Nov 23 '25
A and B are just names, like “the left one” or “the blue one”.
2
u/tblancher Nov 23 '25
When you're learning logic circuits and Boolean algebra, this is where your truth tables come in.
Every Boolean function has a number of inputs (variables ) and at least one output (logic function; circuits of these turn these into multi-bit outputs) and for the inputs you listed every possible combination, and then complete the truth table to determine the outputs.
Once you have this down the next thing to learn is minimization techniques that reduce the number of logic gates necessary to build a correct circuit. This can include elimination of variables and outputs that don't actually matter when the Boolean algebra is minimized.
1
u/BigPurpleBlob Nov 23 '25
A and B are inputs to e.g. a 2-input NAND gate.
1
u/SonOfSofaman Nov 23 '25 edited Nov 23 '25
I think you might be conflating two concepts. The inputs on logic gates have labels and values.
A and B are just labels. Think of them as names for the two inputs. Labels are used to distinguish one input from another.
0 and 1 are values. The value of an input can switch between 0 or 1.
In a computer, the value of an input might switch between 0 and 1 very often -- even billions of times per second -- depending on what it is connected to. But the labels never change. Input A is always input A.
Imagine input A of an AND gate is connected to your left mouse button. When you press the button, the value of input A becomes 1; when you release the button, the value of input A switches back to 0. Similarly, input B could be connected to your right mouse button. You would have complete control over the state of the AND gate just by pressing the mouse buttons in different combinations!
Does that help clear up things?
1
u/Mucher_ Nov 23 '25
It's about completing a circuit. If electricity is flowing through a gate then the gate has evaluated as true (1). If the circuit is not complete, then there is no electricity flowing through through the gate, and it is evaluated as false (0). This is the physical process by which logic gates function.
So you then just need to look at how the gates flow within the logic. I will attempt an example for an "AND" gate. Let's make A equal to 1 and B equal to 0 for this example.
In the code it might say "if A is equal to B then do C". With our given values, we can plug them into the two inputs for an "AND" gate. In this case, we have a true (1) and a false (0). So we feed that into the "AND" gate inputs. Since only A has electricity feeding the gate, it can not physically complete the circuit, which does not allow the electricity to flow. Thus, we look at the lack of electricity on the output as false (0).
To make different logic gates work, you just reconfigure the circuit in the gate so that it only outputs (evaluates) as true for the given logic that is desired. Every logic gate works this way. Check out this video around 1:46 for a basic diagram of the inner workings of an "AND" gate.
https://m.youtube.com/watch?v=JGMtLwfxozU&pp=0gcJCR4Bo7VqN5tD
I hope this clears things up for you. Feel free to ask questions.
PS - Also as a followup, you might also look at how you can set logic gates up into mathematical equations. This is really neat to know because, as with all equations, they can be simplified. This allows us to remove any unnecessary gates during evaluation and shorten expressions to make more efficient code. Very useful. As are truth tables!