Mangcoding

icon chat
Mulida Asti - Tuesday, 4 March 2025 - 8 months ago

Logic Gates (Must Be Understood Before Learning Programming)

single image

Photo by Tianshu Liu on Unsplash

Budi is a smart and diligent person, but he’s forgetful. One day, Budi planned to propose to Wati. Long story short, Wati’s parents agreed to marry Wati to Budi on the condition that:

  1. Budi must have a job
  2. Budi must bring a dowry of at least 5 grams of gold
  3. Wait for Wati to finish her master’s degree (approximately 2 more years

According to Wati’s parents, these conditions are absolute and must be fulfilled. Therefore, Budi has to agree to all these requirements.

A year later, Budi now has a job and has saved up 100 grams of gold for the dowry. Feeling very confident and brave, Budi returned to Wati’s parents. Do you know what happened next? Did Wati’s parents accept Budi?

The story above is an example of how Logic Gates are applied in everyday life. If we observe closely, logic gates are actually not that difficult to understand. However, some people do struggle with them. Before we answer the question from the story above, let’s first learn about logic gates.

Link Mangcoding_Logic Gates

What Are Logic Gates?

Logic gates are the basic foundation of Digital Electronic Systems that function to convert one or more inputs into an output signal.

Logic gates operate based on the binary number system, which uses only 2 symbols: 0 (False) 1 (True) using Boolean Algebra Theory.

Link Mangcoding

Basic Logic Gate Operations

A logic function or logic operation is a combination of binary variables, such as the inputs and outputs of a digital circuit. All logical relationships between binary variables can be explained by three basic logic operations: AND and, OR and NOT. For a clearer explanation, see the descriptions below.

Link Mangcoding_Logic Gates

AND Gate

The AND gate is a relational gate consisting of at least two inputs. AND is absolute, meaning all conditions must be met to produce a True value.

In summary, an AND gate will only produce True if all conditions are fulfilled. If even one condition fails, the result will be False.

A B output
T​ T​ T​
T​ F​ F​
F​ T​ F​
F​ F​ F​

In programming languages, the AND gate is usually represented by the symbol &&amp.

Example cases:

nilai a = 3, b = 4
a>3 && b=4 (False)
a>1 && b<4 (False)
a>=3 && b=4 (True)
a>=1 && b<5 (True)
a+b<10 && a-b <0 (please answer in the comments column)

Link Mangcoding_Logic Gates

OR Gate

The OR gate is a relational gate consisting of at least two inputs. Unlike AND, OR provides multiple options where just one condition being met is enough to result in True.

In summary, an OR gate will only produce False if none of the conditions are met. If even one is fulfilled, the result is True.

A B output
T​ T​ T​
T​ F​ T
F​ T​ T
F​ F​ F​

In programming languages, the OR gate is usually represented by the symbol || (double pipe).

Example cases:

nilai a = 3, b = 4
a>3 || b=4 (True)
a>1 || b<4 (True)
a>3 || b>4 (False)
a>0 || b<0 (True)
a+b<0 || a-b >0 (please answer in the comments column)

Link Mangcoding_Logic Gates

NOT Gate (Negation or Reversal)

The NOT gate is a gate that reverses the value of an input. For example, if an input is True, it will become False. So, the NOT gate produces False if the input is True, and produces True if the input is False.

Input output
T​ F
F T

In programming languages, the NOT gate is usually represented by the symbol ! (exclamation mark).

Example cases:

nilai a = 3, b = 4
!(a>3) && b=4 –> !(False) && (True) get (True)
a>1 || !(b<4) –> (True) || !(False) get (True)
!(a>3) || !(b>4) (please answer in the comments column)
!(a>0 || b<0) (please answer in the comments column)
!(a+b<0 && a-b >0) (please answer in the comments column)

That’s a brief explanation of the basic logic gates. This material is essential to understand before learning any programming language. A program is a tool we create to process various forms of input to produce an output.

If we don’t understand this material, we will struggle to figure out how outputs are generated from a collection of inputs. See you in the next lesson. Keep up the spirit and never stop learning! ^_^

Link Copied to Clipboard