LECTURE 1-Digital circuit
LECTURE 1-Digital circuit
(UD)
DIGITAL CIRCUITS
By
Dr. Yasmin Nasser Mohamed
1
Introduction to Digital Circuits
Why is this course necessary?
• We live in a digital world.
o Almost all electronic devices are made up of digital circuits.
o Software plays a vital role in our daily lives.
o Computer programs process digital data.
o Programs run on computers, which consist of digital circuits.
o Many technologies and systems we rely on are based on digital data and the
digital systems that process and store them.
AI, machine learning, decision-making, etc.
2
Introduction to Digital Circuits
https://semiconductor.samsung.com/
https://upload.wikimedia.org/
dram/module/ https://newsroom.intel.de/news-
wikipedia/commons/a/a3/AM
releases/ 3
D_Ryzen_9_7900X.jpg
Introduction to Digital Circuits
Analog – Digital Signals:
Nature of Signal:
In the real world (where we live), many physical quantities (current, voltage, temperature, light intensity, weight of a person, etc.) vary over a
continuous range.
These signals, which can take any possible value between the limits of such a range, are called analog signals.
Analog Signal: It is a continuous signal that represents physical measurements. Analog signals are used to represent data in a continuous range
and can take any value within a given range. They are susceptible to degradation by noise over distance and time. An analog signal has a
theoretically infinite resolution.
Digital Signal: It consists of discrete values, typically binary, representing zeros and ones. Digital signals process data as a sequence of discrete
levels and are generally more resistant to noise, making them more reliable for long-distance transmission and storage. A binary digital signal
may at any moment take on only one of two possible values:
0 - 1, high - low, on - off, True-false, open - closed.
Representation:
Representation:
5
Introduction to Digital Circuits
Voltage 0, 1
Text and, or
Temperature not
Speed z = a b+c
Image Mathematical World
Switch Model
(Boolean Algebra) HDL Program
(Hardware Description
Real World Language)
(Physical world, WHEN(A & B) THEN X1-
C; ELSE X2 – E # F;
Problem Domain)
X2 – (!A # C);
……
Abstraction
Coding and
Design Implementation
modelling
6
Introduction to Digital Circuits
The Coding of Integers
In this course, we will deal with INTEGERS.
In numeral systems, every single digit when combined constructs a number.
Radix=> R (R is a positive integer)
Radix or base is the number of unique digits, including zero, used to represent numbers in a positional numeral
system. For example, the decimal system (base-10) uses ten digits (0 through 9), and the binary system (base-2)
uses two digits (0 and 1).
R={0,1,2,3,4,5,6,7,8,9}=> radix-10
Example:
1) If R=10, then the number 547 in radix-10 system can be written as:
54710 = 5x102 +4x 101 +7x 100
R={0,1}=> radix-2
•Example: The binary number 10102
Calculation
10102 = 𝟏 × 𝟐𝟑 + 𝟎 × 𝟐𝟐 + 𝟏 × 𝟐𝟏 + 𝟎 × 𝟐𝟎 = 𝟖 + 𝟎 + 𝟐 + 𝟎 = 𝟏𝟎𝟏𝟎
R={0,1,2,3,4,6,7}=> radix-8
Example: The octal number 1758 7
2 1 0
Calculation: 1 × 8 + 7 × 8 + 5 × 8 = 64 + 56 + 5 = 𝟏𝟐𝟓
Introduction to Digital Circuits
BCD (Binary Coded Decimal) Coding System:
Hexadecimal (Radix-16):
• A four-bit pattern represents each decimal number between 0
•Base: 16 and 9.
•Digits used: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F Natural BCD:
(where A=10, B=11, C=12, D=13, E=14, F=15) Number BCD Number: BCD
•Example: The hexadecimal number 1𝐴316 : Code: Code:
Example:
•Calculation: 1 × 162 + 𝐴 × 161 + 3 × 160 = 256 + 0:
1:
0000
0001
5:
6:
0101
0110 Number: 8 0 5
160 + 3 = 41910 2: 0010 7: 0111 BCD : 1000 0000 0101
3: 0011 8: 1000
4: 0100 9: 1001
• It is a redundant code.
Using 4 bits, 16 different code values can be created, but only 10 of them are used.
• Performing arithmetic operations on BCD numbers require more complex circuits than other representation
methods.
Therefore, modern computers don't use BCD coding for numbers.
3. General Conversion Principle:
Each number system can be converted into another by decomposing the number into a sum of its digits multiplied by the base
raised to the power of the digit's position, counting from right to left starting at zero.
Applications: 8
Introduction to Digital Circuits
•Binary: Used in computing and digital electronics for its simplicity in representing electrical states (off or on).
•Decimal: Commonly used in daily life and general arithmetic.
•Octal: Previously used in computing because of its simplicity in converting to and from binary.
•Hexadecimal: Widely used in computing and programming as it offers a more compact representation of binary values and is
easier to convert to and from binary.
Understanding these different bases is crucial in fields ranging from computer science and electronics to mathematics and
engineering, as different systems utilize different bases for calculations and data representation.
Binary Digital Coding: 3742
• As digital systems operate on binary digital signals, they can process -17 3.14
only two values (binary data): ON-OFF, LOW-HIGH, 0-1.
• Therefore, physical quantities (voltage, temperature, etc.) and any kind
of data (letters, numbers, colors, sounds) must be converted to binary
code words (digitally coded) before digital circuits can process
them. 01101101
• Binary code words consist of bits (binary digits). 00100110
• A bit takes on one of two possible values (0 or 1). Coding 11110011
Real World
• Binary: "Something made of two things or parts ". Bini = Two in Latin 00000011
….
oFor example, using 4 bits, 24 (16) code words can be generated to represent 16 different “things”:
0000, 0001, 0010, 0011, 0100, 0101, 0110, 0111,
1000, 1001, 1010, 1011, 1100, 1101, 1110, 1111
oUsing 8-bit code words, we can represent 28 (256) different “things”:
00000000, 00000001, 00000010, … , 11111101, 11111110, 11111111.
oThese can be 256 different colors, 256 symbols, integers between 0 and 255, integers from 1 to 256, or integers
between -128 and +127.
• There are different coding systems (methods) for different types of data.
• The meaning of a binary value (for example, 10001101) is determined by the system (hardware or software) that
processes this data.
This value may represent a number, a color, or another type of data.
• The coding of numbers is especially important. Therefore, in this course, we will give some basic information about the
coding methods of integers. 10
Introduction to Digital Circuits
Examples of Digital Coding:
RGB Color System:
• All colors are constructed from a combination of Red, Green, and Blue colors.
• 8 bits are used to represent the 256 levels of each of these three primary colors. This makes 256*256*256 = 16,777,216
possible colors.
• Examples:
Black: R = 00000000 G = 00000000 B = 00000000
Purple: R = 00001011 G = 00001111 B = 00001011
11
Introduction to Digital Circuits
Examples of Digital Coding (contd):
Tokenization in AI systems built on large language models (LLMs):
• Large language models such as ChatGPT, Gemini, Claude, and Llama convert texts to a set of tokens that the model can
process.
• A token can be a character, a word, a part of a word, or a whole phrase.
• Tokens are transformed into integers the LLM can process.
• A token of an LLM typically contains 16 or 32 bits, depending on the encoding used.
13
Introduction to Digital Circuits
Converting unsigned binary integer to decimal:
Decimal value = bn-1 2n-1 + bn-2 2n-2 + . . . + b2 22 + b1 21 + b0 20
weights
Example: 8-bit unsigned integer (n = 8)
(1101 0111)2 = 127 + 126 + 025 + 124 + 023 + 122 + 121 + 120 = 21510
Converting decimal to unsigned binary:
• Divide the given decimal number recursively by 2 until the quotient is 0.
• The remainders are the bits of the binary integer in the reverse order.
• The remainder of the first division is the Least Significant Bit of the binary integer.
Example: 21510
215/2 = 107 remainder 1 Low-order bit “Least Significant Bit – LSB” (rightmost bit)
107/2 = 53 remainder 1
53/2 = 26 remainder 1
26/2 = 13 remainder 0 21510 = (1101 0111)2
13/2 = 6 remainder 1
6/2 = 3 remainder 0
3/2 = 1 remainder 1 High-order bit “Most Significant Bit – MSB”) (leftmost 14
1/2 = 0 remainder 1 bit)
Introduction to Digital Circuits
Representation of Unsigned Integers (cont'd):
Remember: Decimal (base 10), weighted (positional) coding:
Value = dn-1 10n-1 + dn-2 10n-2 + . . . + d1 101 + d0 100 , diD = {0,1,2,3,4,5,6,7,8,9}
Example: 3-digit decimal integer
(704)10 = 7102 + 0101 + 4100
• The smallest signed positive integer that can be represented with n bits: 0
Examples:
oFor n = 8, 0000 00002 = 010
oFor n = 16, 0000 0000 0000 00002 = 010
16
Introduction to Digital Circuits
Representation of Signed Integers:
• The (high-order) most significant bit denotes the sign of the number.
oPositive numbers start with a “0”,
oNegative numbers start with a “1”.
Positive integers:
• In computers, positive integers are represented (like unsigned integers) using "natural
binary weighted (positional) coding”.
• Remember: Positive binary numbers must start with 0.
Examples of Positive Numbers:
17
Introduction to Digital Circuits
Negative integers (2's complement):
• Negative integers are represented using 2's complement.
• In this representation, negative integers are represented by the 2's complement of
the positive number (absolute value).
To obtain the 2’s complement:
• First, invert (1's complement) the number. Change 0 to 1, 1 to 0.
• Then, add 1 to the inverted number.
2's complement of (𝐴) = ̅ + 1 𝐴 denotes 1's complement of A.
Example:
• Using 4 bits, we can represent signed decimal numbers between
−8 and +7 ((−23 ) and +(23 – 1)).
• The number -8 can be represented with 4 bits: -8 = 1000.
• However, +8 cannot be represented with 4 bits.
• If we take the 2’s complement of 1000 (-8) to obtain +8, the result is 1000.
• However, 1000 cannot be a positive number because it starts with 1.
4-bit -810 : 1000
1's complement : 0111
Add 1 : +0001
Result: ? : 1000
If we take the 2's complement of 4-bit “-8”, we find its 4-bit magnitude (unsigned), i.e., 1000 = (unsigned) 8 (special
case for 4 bits).
21
Introduction to Digital Circuits
Digital Circuits
Special cases in 2’s complement representation (cont'd) Example (contd):
• To obtain the 2's complement 4-bit binary number for the decimal number -8, we also start with
the 4-bit magnitude (unsigned absolute value) of 8.
• We start with the unsigned absolute value of 8. Its UNSIGNED 4-bit binary number equivalent
is 1000.
8 (4-bit magnitude) : 1000
1's complement : 0111
Add 1 : +0001
Result: -8 (4-bit) : 1000
Based on this information, we can update the explanation about the 2's complement
operation as follows:
22
Introduction to Digital Circuits
Representing signed numbers on a circular graphic: We can represent signed numbers on a number wheel.
Example: 4-bit numbers:
23
Introduction to Digital Circuits
The standard coding method for integers in computer systems:
• The standard (default) coding (representation) method for negative integers is 2’s complement.
• For example, when you define a signed integer in a high-level programming language, such as C++, Java, etc.,
the compiler uses 2’s complement to store integers in computer memory.
• Normally, we state explicitly that the method is 2’s complement.
• However, if the coding system for integers is not mentioned clearly, you may assume that 2’s complement is
used.
Programming Examples:
• Examine the following programs to discover how integers are represented in C++.
integer_8.cpp and
integer_16.cpp
• Note: The programs were written using the std module defined in C++23.
If your compiler does not support the standard C++23, you can run the programs with the suffix
"_before20" instead.
24
Introduction to Digital Circuits
The reasons for using the 2’s complement representation:
• Why not to use the "sign-magnitude" representation, which seems simpler and more intuitive?"
oSign-magnitude method: The leftmost bit (MSB) represents the sign of the number (0 for
positive, 1 for negative), and the rest of the bits represent the magnitude of the number.
Example: 8-bit +510 : 0000 0101 8-bit -510 : 1000 0101 (sign-magnitude)
There are two representations for zero! There is a positive zero and a negative zero. +010 : 0000
0000 -010 : 1000 0000 (sign-magnitude)
oThe range is: - (2n-1 -1) to + (2n-1-1) Compare to 2's complement method
25
Introduction to Digital Circuits
Extension (Sign Extension) of Binary Numbers
• In digital systems, a certain number of bits (memory locations) are allocated for binary numbers, e.g., 16 bits, 32 bits, etc.
• In some cases, it is necessary to write a number to a memory space with more bits than necessary (for example, 8-bit number
to 16-bit memory).
• Sometimes, it is necessary to operate on two numbers of different lengths.
• In such cases, the shorter number is extended (word length is increased). For example, extension from 4 bits to 8 bits or
from 8 bits to 16 bits.
• The extension operation is different for unsigned and signed numbers.
Unsigned Numbers: The high-order part of the binary number is filled with "0"s.
Example: 4-bit 310 = 0011 8-bit 310 = 0000 0011
Example: 4-bit 910 = 1001 8-bit 910 = 0000 1001
Signed Numbers: The high-order part of the binary number is filled with the value of the sign bit. This operation is called sign
extension.
26
Introduction to Digital Circuits
Hexadecimal (Base-16) Numbers
• Binary numbers are necessary because digital
circuits can directly process binary values. 0 0000 0
• However, it is difficult for humans to work with large numbers of bits in even 1 0001 1
relatively small binary numbers. (Example: 1110010001011010) 2 0010 2
• Therefore, for documentation (to write and read easily), hexadecimal (base-16) 3 0011 3
numbers are used. 4 0100 4
• Converting from binary to hexadecimal is easy: 5 0101 5
6 0110 6
oEach hex. digit maps to 4 bits. See the table.
7 0111 7
oSeparate binary numbers into groups of 4 bits. 8 1000 8
oFind the single hexadecimal digit corresponding to each group of 4 bits. 9 1001 9
Examples: 10 1010 A
• Binary Hexadecimal 11 1011 B
010111012 = 0101 1101 (Binary) 12 1100 C
13 1101 D
= 5 D (Hexadecimal)
14 1110 E
• Hexadecimal Binary 15 1111 F
$87 = 1000 01112
• To express hexadecimal numbers, the symbols $ and h are usually used.
27
Example: $5D , $87 or 5Dh , 87h.
Introduction to Digital Circuits
Addition and Subtraction Operations in Digital Systems
• In computers, the Arithmetic Logic Unit (ALU) performs the integer arithmetic operations.
• As an advantage of 2's complement representation, integer addition and subtraction operations are
performed on unsigned and signed numbers in the same way.
• However, the result is interpreted differently for unsigned and signed numbers.
• Before an operation on numbers of different lengths, a sign extension is necessary.
oRemember: The extension operations are different for unsigned and signed numbers.
Addition:
• The result of the addition of two bits (a and b) is a 2-bit number.
• The LSB of the result is the 1-bit sum, and the MSB is the carry.
• The rules of binary addition are given in the table on the right. Inputs Result
a b Carry Sum
0+0 0 0
0+1 0 1
1+0 0 1
1+1 1 0
28
Introduction to Digital Circuits
Addition:
Unsigned Integers:
The result of the addition of two n-bit unsigned numbers can be a (n+1)-bit number. The
(n+1)st bit is called the carry.
Examples: Addition of two 8-bit unsigned numbers
01110101 : 117
+ 01100011 : Only for
99 testing
11011000 : 216
No Carry (Carry=0)
31
Introduction to Digital Circuits
Subtraction:
• Computers usually use the method of complements to implement subtraction.
• 2's complement of the second operand is added to the first number.
𝐴 − 𝐵 = 𝐴 + (−𝐵
= 𝐴 + 2 ′𝑠)𝑐𝑜𝑚𝑝𝑙𝑒𝑚𝑒𝑛𝑡 (𝐵)
=𝐴+ 𝐵 + 1
• So, only one addition circuit is sufficient to perform both addition and subtraction (benefit of 2's
complement system).
• In the following Section, we will cover addition and subtraction circuits.
• Like addition, subtraction operations are performed on unsigned and signed integers in the same way
(because of 2's complement representation). However, the interpretation of the result is different for
unsigned and signed integers.
32
Introduction to Digital Circuits
Subtraction (cont'd):
Unsigned Integers:
If the result of subtracting two n-bit unsigned numbers, performed using 2's complement, is a (n+1)-bit
number (there is a carry), then there is no borrow, and the result is valid.
If the (n+1)st bit of the result is zero (no carry), the first operand is smaller than the second, and there
is a borrow.
Carry = 1 no borrow
Carry = 0 borrow
Examples: Subtraction of 8-bit unsigned numbers
2's complement
00000101: 5 00000101: 5
- 00000001: 1 + 11111111:-1
100000100: 4
Carry=1 : No Borrow
00000001: 1
- 00000101: 5 2's complement
00000001: 1
+ 11111011:-5
011111100: Cannot be
represented
No Carry: Borrow 33
Introduction to Digital Circuits
Subtraction (cont'd):
Signed Integers:
Subtraction on signed integers is also performed using 2's complement.
Overflow: Just as in addition, an overflow can occur in subtracting signed integers. In subtraction,
overflow can occur in two cases:
positive – negative negative and negative – positive positive
Example:
11111101: -3 11111101:-3
- 01111111:+127 2’s complement +10000001:-127
101111110: cannot be represented
Neg. – pos. = pos. Overflow Sign:0, result: positive. 34
Introduction to Digital Circuits
Comparing integers:
The subtraction operation is used to compare two integers.
After the operation R = A - B, related flags (status bits: carry, overflow) are checked.
Unsigned Integers:
Signed Integers:
Result (R) Overflow Comparison
=0 X (not important) A=B
Positive, ≠0 NO A>B
Negative NO A<B Because of overflow, the sign of the
Positive YES A<B result is not correct.
Negative YES A>B
35
Introduction to Digital Circuits
Summary of Carry, Borrow, Overflow
Carry: It can occur in the addition of unsigned integers.
It indicates that the result cannot be represented with n bits, and an (n+1)st bit is necessary.
Borrow: It can occur in the subtraction of unsigned numbers (A - B).
It indicates that the first number (A) is smaller than the second one (B, the number being subtracted), i.e.,
A < B.
The result cannot be represented with unsigned numbers.
If the result of the subtraction using 2's complement is an n-bit number (no carry), then there is a borrow, and
the result is invalid.
Overflow: It can occur only on signed numbers in addition and subtraction operations.
It indicates that the result cannot be represented with n bits.
Overflow can be detected by checking the signs of operands and the result. There is an overflow in the
following cases:
pos. + pos. neg. pos. – neg. neg.
neg. + neg. pos. neg. – pos. pos.
36
Introduction to Digital Circuits
Summary of Carry, Borrow, Overflow
Unsigned Signed
Event Carry Borrow Overflow
Affected Addition Subtraction n-bit Addition/Subtraction n-bit
Operation n-bit integers integers integers
Meaning The result cannot A < B, The result cannot be
be represented The result cannot be represented with n bits. The
with n bits. represented with sign of the result is inverted.
Carry can be used unsigned numbers.
to interpret the
result.
Detection (n+1)st bit of the If the result is an n-bit Check the signs of operands
result is 1. number (Carry=0), and the result
Carry = 1 there is a borrow, and pos. + pos. -> neg.
the result is invalid. pos. – neg. -> neg.
neg. + neg. -> pos.
neg. – pos. -> pos.
37
Introduction to Digital Circuits
Digital Coding-Related Definitions:
We define some coding-related terms that we will use in the following chapters.
Hamming distance: The Hamming distance between two n-bit long code words is the number of bit positions at
which the corresponding bits are different.
Example: Hamming distance between 011 and 101 is 2.
Richard Wesley Hamming (1915-1998) Mathematician, USA
Adjacent code words: If the Hamming distance between two code words is 1 (only one bit changes), these code
words are adjacent. Example: 0101 - 1101
Adjacent coding systems: Between each pair of successive code words, the Hamming distance is 1 (only one bit
changes).
In addition, if the Hamming distance between the first and last code words is 1, then this code is called cyclic
(circular).
Example: A cyclic BCD code (different from natural BCD !)
Number: Code: Number: Code:
0: 0000 5: 1110
1: 0001 6: 1010
2: 0011 7: 1000
3: 0010 8: 1100
4: 0110 9: 0100 38
Introduction to Digital Circuits
Gray Code: A binary (base 2), nonredundant, and cyclic (also adjacent) coding system that
represents 2n elements is called a Gray code.
39
40
41