Perceptron for Simulating an AND Logic Gate
Idea
This code was developed as a practical exercise based on a YouTube video by @enkk.
In the video, Enkk explains in a simple and intuitive way what a perceptron is, using a practical and easily understandable example. In this post, we implement the perceptron code.
Useful Links:
Needless to say, following @Enkk is highly recommended if you are interested in studying Large Language Models (LLMs) and AI.
The Automatic Gate Analogy
The example used in the video is particularly effective: imagine a parking system where a car can enter only if both gates (Gate A and Gate B) are open at the same time… OBVIOUSLY! ;-)
Logical Scheme of the Example Perceptron
System Logic (Example from @Enkk):
- Gate A open = signal 1
- Gate B open = signal 1
- Output 1 = The car may enter
- Output 0 = The car may not enter
Truth Table (AND):
Gate A | Gate B | Result | Action |
---|---|---|---|
0 (closed) | 0 (closed) | 0 | ❌ Car does not enter |
1 (open) | 0 (closed) | 0 | ❌ Car does not enter |
0 (closed) | 1 (open) | 0 | ❌ Car does not enter |
1 (open) | 1 (open) | 1 | ✅ Car may enter |
Connection with Digital Logic
This analogy perfectly represents the behavior of an AND logic gate, where the output is 1 only when both inputs are 1. In all other cases, the output is 0.
Why Use a Perceptron?
It’s important to note that implementing a simple AND gate requires only a few lines of code:
int porta_and(int a, int b) {
return a && b; // Very simple!
}
…even simpler:
#include <stdio.h>
int main(void){
int a,b;
a=0; // Assign value 1 or 0
b=0; // Assign value 1 or 0
if (a && b){
printf("Valore output: 1\n");
}
else{
printf("Valore output: 0\n");
}
}
What is a Perceptron?
The perceptron is the simplest and most fundamental form of an artificial neural network. It is a single artificial neuron that makes decisions based on the data it receives as input.
Basic Structure
A perceptron is made up of:
- Inputs: The data it receives (in our case, the state of the two gates)
- Weights: Numbers that determine the importance of each input (VERY IMPORTANT: weights are the parameters of a Large Language Model, i.e., its intelligence)
- Bias: A threshold value that helps with the final decision
- Activation Function: Decides whether to “activate” the neuron (output 1) or not (output 0)
- Output: The final decision (1 or 0)
Why is the perceptron the smallest element of a Neural Network?
The perceptron is like the “brick” used to build more complex neural networks:
- Simple Neural Network: 1 perceptron or a few perceptrons (nowadays only for didactic purposes)
- Multilayer Neural Network: Hundreds or thousands of perceptrons connected together
- Deep Learning: Millions of perceptrons organized into layers
Just as you need to understand how a single brick works before building a house, to understand neural networks it’s useful to first master the perceptron.
Why is it Important to Understand How It Works?
- Theoretical Foundation: All advanced concepts (backpropagation, gradient descent, etc.) are derived from perceptron principles.
- Didactic Simplicity: Simple enough to fully understand, but contains all the key concepts and allows you to build a mental model.
- Historical Basis: It was the first artificial neuron model.
- Intuitive Understanding: Once you understand the perceptron, the conceptual leap to more complex networks becomes much more natural.
Didactic Value of This Exercise
Using a perceptron for this example may seem excessive, but it represents an excellent educational exercise for several reasons:
- Simple Problem: The AND gate is easy to visualize.
- Verifiable Result: It’s easy to check whether it works (I added printouts in the code step by step).
- Complete Concepts: Includes all fundamental elements (training, weights, bias, epochs).
- Conceptual Bridge: Links basic digital logic to advanced machine learning.
1. Understanding the Fundamentals of Neural Networks
- Weights: How the network assigns importance to inputs
- Bias: The threshold term that influences the final decision
- Activation Function: How binary outputs are produced
2. Learning Process
- Training Epochs: Repeated learning cycles
- Dataset: Collection of input-output examples for training
- Weight Updates: How the network “learns” from its mistakes (VERY IMPORTANT)
3. Learning Algorithm
- Forward Pass: Prediction calculation
- Error Calculation: Comparing with the expected output
- Backward Pass: Updating parameters based on error
4. Convergence Concepts
- How the network reaches an optimal solution
- Stopping criteria for training
- Performance evaluation
Implementation
NOTE: This is version 1 of the implementation. I will probably update the code on GitHub as I come up with new ideas. Always refer to the GitHub project for the latest version.
GitHub link to the implementation
/*
* PERCEPTRON
* (AND Logic Gate Learning)
*
* Author: Vincenzo Argese
* Web: https://cr1s1um.github.io/
* Date: 2025-08-09
* Version: v1.0
* Idea: Youtuber @Enkk video “come funziona: le reti neurali (pt.1)” https://www.youtube.com/watch?v=2UdQQA65jcM
*
* Description:
* This program implements a simple perceptron (single artificial neuron) that learns
* to simulate an AND logic gate through supervised learning.
*
* The perceptron receives two binary inputs (0 or 1) and learns to output:
* - 1 only when both inputs are 1 (like an AND gate)
* - 0 in all other cases
*
* Training process:
* 1. Initialize weights and bias with small random values
* 2. For each training example, calculate prediction
* 3. Compare prediction with expected output
* 4. If wrong, adjust weights using perceptron learning rule
* 5. Repeat until all predictions are correct or max epochs reached
*
* This demonstrates fundamental neural network concepts: weights, bias,
* activation functions, training epochs, and supervised learning.
*/
#include <stdio.h>
#define EPOCHS 100 // Define constant: maximum number of training epochs
/* Forward declaration of activation function */
int activation_function(float sum);
int main(void)
{
/* Training Dataset */
int
x[4][2]={ // Input matrix: 4 examples with 2 features each
{0,0}, // First example: x1=0, x2=0 -> expected output: 0
{1,0}, // Second example: x1=1, x2=0 -> expected output: 0
{0,1}, // Third example: x1=0, x2=1 -> expected output: 0
{1,1} // Fourth example: x1=1, x2=1 -> expected output: 1
},
y[4]={0,0,0,1}; // Expected outputs: represents AND logic function
/* Weights and Bias initialization */
float
w1 = 0.1, // Weight for first input, initialized to 0.1
w2 = 0.1, // Weight for second input, initialized to 0.1
b = 0.1; // Bias term, initialized to 0.1
// Learning rate parameter
float learning_rate = 0.1; // Controls how much weights are adjusted during learning
/* Training Phase */
for (int epoch=0; epoch < EPOCHS; epoch++){ // Main training loop: up to 100 epochs
int errors = 0; // Counter for errors in current epoch
// Process all training examples
for(int i=0; i<4; i++){ // Iterate through all 4 examples in dataset
// DEBUG - Print current training state
printf("TRAINING EPOCH: %d\n", epoch); // Fixed typo: epoch instead of ephoc
printf("Current errors: %d\n", errors); // Print current error count
printf("Input X1: %d - X2: %d\n", x[i][0], x[i][1]); // Print current inputs
printf("Expected output Y: %d\n", y[i]); // Print expected output
printf("w1: %.1f - w2: %.1f - Bias: %.1f\n", w1, w2, b); // Print current weights and bias
printf("Learning Rate: %.1f\n", learning_rate); // Print learning rate
// Forward pass: calculate weighted sum
float weighted_sum = (x[i][0]*w1 + x[i][1]*w2) + b; // Linear combination: x1*w1 + x2*w2 + bias
int predicted_output = activation_function(weighted_sum); // Apply activation function
// DEBUG - Print forward pass results
printf("Weighted sum: %.1f\n", weighted_sum); // Print the weighted sum
printf("Predicted output: %d\n", predicted_output); // Print network's prediction
// Calculate prediction error
int error = y[i] - predicted_output; // Error = expected - predicted
// DEBUG - Print error information
printf("Prediction error: %d\n", error); // Print the error
printf("---------------------------------------------\n"); // Separator line
// Update weights if there's an error (perceptron learning rule)
if (error != 0){ // Only update if prediction is wrong
// Apply perceptron learning rule
w1 += learning_rate * error * x[i][0]; // Update w1: w1 = w1 + η * error * x1
w2 += learning_rate * error * x[i][1]; // Update w2: w2 = w2 + η * error * x2
b += learning_rate * error; // Update bias: b = b + η * error
errors++; // Increment error counter
}
}
// Check convergence: stop if no errors occurred
if(errors == 0){ // Perfect classification achieved
printf("TRAINING COMPLETED - epoch: %d\n", epoch); // Print completion message
break; // Exit training loop early
}
}
// Print final results
printf("\n\n"); // Print empty lines for readability
printf("=== FINAL TRAINED PARAMETERS ===\n"); // Print results header
printf("Final w1: %.1f - w2: %.1f - Bias: %.1f\n", w1, w2, b); // Print final weights and bias
// Test the trained perceptron
printf("\n=== TESTING TRAINED PERCEPTRON ===\n");
for(int i=0; i<4; i++){
float test_sum = (x[i][0]*w1 + x[i][1]*w2) + b;
int test_output = activation_function(test_sum);
printf("Input (%d,%d) -> Output: %d (Expected: %d)\n",
x[i][0], x[i][1], test_output, y[i]);
}
return 0;
}
/*
* Activation Function
*
* Purpose: Converts continuous weighted sum to binary output
* Parameter: float sum - the weighted sum from perceptron
* Returns: 1 if sum > 0, otherwise returns 0
*
* This implements a threshold activation function commonly used in perceptrons
*/
int activation_function(float sum){
return (sum > 0) ? 1 : 0; // Ternary operator: if sum > 0 return 1, else return 0
}
Conclusion
An AND gate can be implemented in a much simpler way, but this exercise provides a solid foundation for understanding the fundamental mechanisms behind more complex neural networks. It serves as an ideal bridge between basic digital logic and advanced machine learning concepts.
Every element that may seem “excessive” in this context (weights, bias, training epochs) becomes essential when moving on to more complex problems where traditional solutions are no longer sufficient.