# Unit 2: Digital Logic

## 2.1 Introduction to Digital Computers and Number System

A digital computer is an electronic device that processes data in a discrete, digital form. Unlike analog systems that use continuous ranges of values to represent information, digital computers use binary states: ON and OFF, which are represented mathematically as 1 and 0. This discrete approach allows computers to store, process, and transmit data with high precision and reliability.

At the core of digital systems is the concept of a **Number System**, which is a way to represent numbers. While humans naturally use the decimal system (base-10), digital computers rely on the binary system (base-2) and its closely related cousins, the octal (base-8) and hexadecimal (base-16) systems.

### Binary Numbers
The binary number system is the fundamental language of computers. It uses only two digits: **0 and 1**, which are called bits (short for binary digits). Every piece of data, whether it's text, an image, or a video, is ultimately stored and processed as a sequence of these bits.

In the binary system, the position of each digit represents a power of 2, starting from the rightmost digit (least significant bit) to the leftmost digit (most significant bit).

For example, the binary number $1011_2$ can be understood as:
$(1 \times 2^3) + (0 \times 2^2) + (1 \times 2^1) + (1 \times 2^0) = 8 + 0 + 2 + 1 = 11_{10}$

### Base Conversions
Understanding how to convert between different number systems is crucial for computer scientists and engineers. Let's look at the conversions between Decimal, Binary, Octal, and Hexadecimal systems.

#### 1. Decimal to Binary, Octal, and Hexadecimal
To convert a decimal number to another base, we use the **repeated division method**. We continuously divide the decimal number by the target base (2, 8, or 16) and record the remainders. Reading the remainders from bottom to top gives the converted number.

**Decimal to Binary (Base 10 to Base 2):**
Example: Convert $13_{10}$ to binary.
- $13 \div 2 = 6$, Remainder = 1
- $6 \div 2 = 3$, Remainder = 0
- $3 \div 2 = 1$, Remainder = 1
- $1 \div 2 = 0$, Remainder = 1
Reading remainders from bottom to top: $1101_2$

**Decimal to Octal (Base 10 to Base 8):**
Example: Convert $45_{10}$ to octal.
- $45 \div 8 = 5$, Remainder = 5
- $5 \div 8 = 0$, Remainder = 5
Reading remainders from bottom to top: $55_8$

**Decimal to Hexadecimal (Base 10 to Base 16):**
In hexadecimal, digits 10 through 15 are represented by letters A through F.
Example: Convert $254_{10}$ to hexadecimal.
- $254 \div 16 = 15$, Remainder = 14 (which is 'E')
- $15 \div 16 = 0$, Remainder = 15 (which is 'F')
Reading remainders from bottom to top: $FE_{16}$

#### 2. Binary, Octal, and Hexadecimal to Decimal
To convert a number from base-2, base-8, or base-16 to base-10, we use the **positional weight method**, where each digit is multiplied by the base raised to the power of its position index (starting from 0 on the right).

**Binary to Decimal:**
Example: Convert $1010_2$ to decimal.
- $(1 \times 2^3) + (0 \times 2^2) + (1 \times 2^1) + (0 \times 2^0)$
- $= 8 + 0 + 2 + 0 = 10_{10}$

**Octal to Decimal:**
Example: Convert $57_8$ to decimal.
- $(5 \times 8^1) + (7 \times 8^0)$
- $= 40 + 7 = 47_{10}$

**Hexadecimal to Decimal:**
Example: Convert $2A_{16}$ to decimal.
- $(2 \times 16^1) + (10 \times 16^0)$
- $= 32 + 10 = 42_{10}$

#### 3. Conversions between Binary, Octal, and Hexadecimal
Since 8 ($2^3$) and 16 ($2^4$) are powers of 2, conversions between binary and these bases are straightforward using grouping.

**Binary to Octal:** Group binary digits into sets of 3, starting from the right. Pad with leading zeros if necessary.
Example: $101101_2 \rightarrow (101) (101) \rightarrow 55_8$

**Octal to Binary:** Replace each octal digit with its 3-bit binary equivalent.
Example: $34_8 \rightarrow (011) (100) \rightarrow 011100_2$

**Binary to Hexadecimal:** Group binary digits into sets of 4, starting from the right.
Example: $11010110_2 \rightarrow (1101) (0110) \rightarrow D6_{16}$

**Hexadecimal to Binary:** Replace each hex digit with its 4-bit binary equivalent.
Example: $1F_{16} \rightarrow (0001) (1111) \rightarrow 00011111_2$


## 2.2 Working of Logic Gates

Logic gates are the basic building blocks of any digital system. They are electronic circuits that perform basic logical operations on one or more input signals to produce a single output signal. The relationship between the input(s) and output is described by a truth table.

Here are the primary logic gates:

### 1. AND Gate
The AND gate produces a high output (1) only if **all** of its inputs are high (1). If any input is low (0), the output is low (0). It performs logical multiplication.
- **Symbolic Expression:** $Y = A \cdot B$
- **Truth Table:**

| A | B | Y |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 0 |
| 1 | 0 | 0 |
| 1 | 1 | 1 |

### 2. OR Gate
The OR gate produces a high output (1) if **any** or all of its inputs are high (1). It only outputs low (0) when all inputs are low (0). It performs logical addition.
- **Symbolic Expression:** $Y = A + B$
- **Truth Table:**

| A | B | Y |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 1 |

### 3. INVERTER (NOT Gate)
The INVERTER, or NOT gate, has only one input and one output. It produces an output that is the inverse (opposite) of its input.
- **Symbolic Expression:** $Y = \bar{A}$ or $Y = A'$
- **Truth Table:**

| A | Y |
|---|---|
| 0 | 1 |
| 1 | 0 |

### 4. XOR Gate (Exclusive-OR)
The XOR gate produces a high output (1) when its inputs are **different** (one is high and the other is low). If the inputs are the same (both high or both low), the output is low (0).
- **Symbolic Expression:** $Y = A \oplus B$
- **Truth Table:**

| A | B | Y |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 0 |

### 5. XNOR Gate (Exclusive-NOR)
The XNOR gate is the complement of the XOR gate. It produces a high output (1) only when its inputs are **identical** (both high or both low). It is also known as an equivalence gate.
- **Symbolic Expression:** $Y = \overline{A \oplus B}$ or $A \odot B$
- **Truth Table:**

| A | B | Y |
|---|---|---|
| 0 | 0 | 1 |
| 0 | 1 | 0 |
| 1 | 0 | 0 |
| 1 | 1 | 1 |


## 2.3 Working of Universal Gates

A universal gate is a logic gate that can be used to construct all other basic logic gates (AND, OR, NOT). This property makes them incredibly important in digital design because a complex circuit can be built using only one type of gate, which simplifies manufacturing and reduces costs. The two universal gates are the NAND gate and the NOR gate.

### 1. NAND Gate
The NAND gate is a combination of an AND gate followed by a NOT gate (NOT-AND). It produces a low output (0) only if **all** of its inputs are high (1). For any other combination of inputs, the output is high (1).
- **Symbolic Expression:** $Y = \overline{A \cdot B}$
- **Truth Table:**

| A | B | Y |
|---|---|---|
| 0 | 0 | 1 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 0 |

**Why is NAND universal?**
- **NOT from NAND:** By connecting both inputs of a 2-input NAND gate together (so $A=B$), it behaves like a NOT gate ($Y = \bar{A}$).
- **AND from NAND:** By feeding the output of a NAND gate into another NAND gate configured as a NOT gate, we get an AND function.
- **OR from NAND:** By first inverting both inputs using NAND gates configured as NOT gates, and then feeding those inverted signals into a third NAND gate, we get an OR function.

### 2. NOR Gate
The NOR gate is a combination of an OR gate followed by a NOT gate (NOT-OR). It produces a high output (1) only when **all** of its inputs are low (0). If any input is high, the output is low.
- **Symbolic Expression:** $Y = \overline{A + B}$
- **Truth Table:**

| A | B | Y |
|---|---|---|
| 0 | 0 | 1 |
| 0 | 1 | 0 |
| 1 | 0 | 0 |
| 1 | 1 | 0 |

**Why is NOR universal?**
- **NOT from NOR:** By connecting both inputs of a 2-input NOR gate together, it behaves like a NOT gate.
- **OR from NOR:** By feeding the output of a NOR gate into another NOR gate configured as a NOT gate, we get an OR function.
- **AND from NOR:** By first inverting both inputs using NOR gates configured as NOT gates, and then feeding those inverted signals into a third NOR gate, we get an AND function.

Understanding these universal properties is foundational for designing efficient and cost-effective digital electronic circuits.
