---
title: "Perceptron; neural network as XOR gate"
subject: "Artificial Intelligence"
chapter: 6
key: "semester_6/artificial_intelligence:perceptron-xor-gate"
source: https://www.bctengineer.com/q/semester_6/artificial_intelligence/perceptron-xor-gate
site: BCT Engineering
---

# Perceptron; neural network as XOR gate

Artificial Intelligence · Chapter 6: Machine Learning

- Asked in: 2073 Magh, 1+7 marks
- Video: [Perceptron Rule to design XOR Logic Gate Solved Example ANN Machine Learning by Mahesh Huddar](https://www.youtube.com/watch?v=dM_8Y41EgsY) — Mahesh Huddar
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_6/artificial_intelligence/perceptron-xor-gate
- Whole chapter: https://www.bctengineer.com/md/semester_6/artificial_intelligence/chapter-6

## Question

What do you understand by perceptron? How can we design a neural network that acts as an XOR gate?

## Short answer

A perceptron is a linear classifier; a single perceptron cannot realize XOR because it is not linearly separable. XOR requires a multilayer network: hidden layer with h1=(x1 AND NOT x2), h2=(NOT x1 AND x2), and output OR combining them.

## Answer

A **perceptron** is a single neuron that outputs 1 when the weighted sum of its inputs reaches a threshold, otherwise 0; it is a linear classifier.

#### A single perceptron cannot do XOR
For output 1 on (0,1) and (1,0) and 0 on (0,0) and (1,1) we need: 0 < θ, w1 ≥ θ, w2 ≥ θ, w1 + w2 < θ. But w1, w2 ≥ θ > 0 gives w1 + w2 ≥ 2θ > θ, a contradiction. The points are not linearly separable (no single straight line separates them).

#### Two-layer design
XOR = (x1 AND NOT x2) OR (NOT x1 AND x2). Use a hidden layer of two perceptrons and one output perceptron (step: fire if net ≥ 1).
```text
 x1 --(+1)--> [h1] --(+1)-->\
    \  /(-1)                 [ y ]
     \/                      /
     /\  (-1)               /
 x2 --(+1)--> [h2] --(+1)--/
```
- h1: weights x1 = +1, x2 = −1, threshold 1 (computes x1 AND NOT x2)
- h2: weights x1 = −1, x2 = +1, threshold 1 (computes NOT x1 AND x2)
- y: weights h1 = +1, h2 = +1, threshold 1 (h1 OR h2)

| x1 | x2 | h1 net | h1 | h2 net | h2 | y net | y |
|---|---|---|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| 0 | 1 | −1 | 0 | 1 | 1 | 1 | 1 |
| 1 | 0 | 1 | 1 | −1 | 0 | 1 | 1 |
| 1 | 1 | 0 | 0 | 0 | 0 | 0 | 0 |

The outputs 0, 1, 1, 0 match XOR (checked by program).

**Answer:** XOR needs a multilayer perceptron with one hidden layer; the hidden layer transforms the inputs into a linearly separable form. Such networks are trained by back-propagation.

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. Why can a single perceptron not do XOR?
   A. XOR has three inputs
   B. The step function cannot output 0
   C. The learning rate is too high
   D. The inequalities w1>=theta, w2>=theta and w1+w2<theta contradict each other
   Answer: D. w1 + w2 >= 2 theta > theta contradicts w1 + w2 < theta. (Key point: Why single fails)

2. What does hidden neuron h1 (weights +1, -1, threshold 1) compute?
   A. x1 AND NOT x2
   B. NOT x1 AND x2
   C. x1 OR x2
   D. x1 AND x2
   Answer: A. It fires only for x1 = 1, x2 = 0. (Key point: Hidden neuron h1)

3. What does the output neuron (weights +1, +1, threshold 1) compute?
   A. h1 AND h2
   B. h1 OR h2
   C. h1 XOR h2
   D. NOT h1
   Answer: B. XOR = (x1 AND NOT x2) OR (NOT x1 AND x2). (Key point: Output neuron)

4. What architecture is needed?
   A. A single perceptron with a bigger threshold
   B. Two perceptrons without a hidden layer
   C. A multilayer network with a hidden layer, trained by back-propagation
   D. A Hopfield network
   Answer: C. The hidden layer makes the problem linearly separable. (Key point: Network type)

## Close variants set in other exams

- [Perceptron; neural network as OR gate](https://www.bctengineer.com/question.md?key=semester_6%2Fartificial_intelligence%3Aperceptron-or-gate) — Artificial Intelligence, chapter 6, asked 2×

## Related in Artificial Intelligence

- [Role of perceptron in neural network](https://www.bctengineer.com/question.md?key=semester_6%2Fartificial_intelligence%3Aperceptron-role) — Artificial Intelligence, chapter 6, asked 2×
- [Neural network, back-propagation, perceptron](https://www.bctengineer.com/question.md?key=semester_6%2Fartificial_intelligence%3Ann-bp-perceptron) — Artificial Intelligence, chapter 6
- [McCulloch-Pitts network, AND, XOR](https://www.bctengineer.com/question.md?key=semester_6%2Fartificial_intelligence%3Amcculloch-pitts-and-xor) — Artificial Intelligence, chapter 6

## Related in other subjects

- [Short note: Neural network, perceptron rule](https://www.bctengineer.com/question.md?key=semester_7%2Fdata_mining%3Aneural-network-perceptron-note) — Data Mining, chapter 3
- [XOR gate using minimum NAND gates](https://www.bctengineer.com/question.md?key=semester_3%2Fdigital_logic%3Axor-using-nand-only) — Digital Logic, chapter 2, asked 2×
