0% found this document useful (0 votes)
19 views40 pages

Chapter 2 3

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 40

CHAPTER TWO

DIGITAL LOGIC FUNDAMENTALS


STRUCTURE

2.0 Chapter Objectives


2.1 Introduction

2.2 Number Systems


2.2.1 Binary Number System
2.2.2 Octal Number System
2.2.3 Hexadecimal Number System
2.2.4 Binary Arithmetic
2.2.5 Alphanumeric code
2.2.6 Hexadecimal arithmetic
2.2.7 Exercise 1

2.3 Logic Gates and Logic Circuits


2.3.1 AND Gate
2.3.2 OR Gate
2.3.3 NOT Gate
2.3.4 NAND Gate
2.3.5 NOR Gate
2.3.6 XOR Gate
2.3.7 Logic Circuits
2.3.8 Exercise 2

2.4 Glossary
2.5 Answers to Exercise

DIGITAL LOGIC FUNDAM ENTALS Page 1


2.0 Chapter OBJECTIVES
After going through this unit, you will be able to
· understand the decimal, binary, octal and hexadecimal number systems
· convert from one number system into another
· apply arithmetic operations to binary numbers
· understand BCD codes and alpha numeric codes
· learn the operations of logic gates
· xpressions

2.1 INTRODUCTION
The binary number system and digital codes are fundamental to computers.
In this chapter, the binary number system and its relationship to other
systems such as decimal, hexadecimal, and octal are introduced. Arithmetic
operations with binary numbers are discussed to provide a basis for
understanding how computers and many other types of digital systems work.
Also binary coded decimal (BCD), and alpha numeric codes are introduced.

Binary logic gates are explained with the help of logic diagram, block
diagram and truth table.

2.2 NUMBER SYSTEMS


We are writing numbers in a decimal numeral system which uses the digits 0,
1, 2, 3, 4, 5, 6, 7, 8, and 9. Ten is the number which is the count of fingers and
thumbs on both hands. The English word digit as well as its translation in
many languages is also the anatomical term for fingers and toes. In English,
decimal means tenth.

However, this system is too complex to be used by electronic circuits. They


must use a system which represents numeric values by using only two
symbols, 0 and 1. This is very adequate for any electronic device as the values
of 0 and 1 can be easily represented by a low and a high voltage in a circuit
(i.e., by an on/off bulb or an open/closed switch).

DIGITAL LOGIC FUNDAM ENTALS Page 2


2.2.1 Binary Number System

A binary number system is a code that uses only two basic symbols. The digits
can be any two distinct characters, but it should be 0 or 1. The binary
equivalent for some decimal numbers are given below

decimal 0 1 2 3 4 5 6 7 8 9 10
binary 0 1 10 11 100 101 110 111 1000 1001 1010

Each digit in a binary number has a value or weight. The LSB has a value of 1.
The second from the right has a value of 2, the next 4 , etc., If we have a
binary number 11001, then the LSB has value 1 ,the MSB has value 16, and
the number has value 25.
16 8 4 2 1
4 3 2 1
2 2 2 2 20
1 1 0 0 1
MSB LSB

Counting in binary is similar to counting in any other number system.


Beginning with a single digit, counting proceeds through each symbol, in
increasing order. Decimal counting uses the symbols 0 through 9, while
binary only uses the symbols 0 and 1.

Bit & Byte


A binary digit is called a Bit. There are only two possible states in a bit,
usually expressed as 0 and 1. A bit cannot be empty or blank, a bit only can be
0 or 1.

A series of eight bits makes a byte, much as 12 makes a dozen. With 8 bits
there are 28 = 256 possible combinations, so a byte can be 256 possible
values from 0 (0000 0000) to 255 (1111 1111). The unit symbol for the byte is
the upper-case character B, while the bit is represented by a lower-case
character b.

DIGITAL LOGIC FUNDAM ENTALS Page 3


K, M, G,T, & P
The kilobyte is a multiple of the unit byte for digital information. The prefix
kilo means 1000, but the kilobyte is considered to be 1024 (210) bytes in
computer science and information technology. The recommended unit
symbol for the kilobyte is kB or kbyte. Other units can be formed multiplying
by 1024 the previous one.
Symbol Prefix Binary Value Approximate
meaning number
k Kilo 210 1024 bytes 103 bytes
M Mega 220 1024*1024 bytes 106 bytes
G Giga 230 10243 bytes 109 bytes
T Tera 240 10244 bytes 1012 bytes
P Peta 250 10245 bytes 1015 bytes

QUESTION 1: How many CD-ROM of 700 MB can be stored in an USB memory


of 8 GB?
QUESTION 2: How long does it take downloading a film of 1 GB through an
ADSL line with a speed of 3 Mbit/s
Binary to decimal conversion:
(1001)2 = (…………….)10
1001 = 1x23 + 0x22 + 0x21 +1x20
= 8+0+0+1
= (9)10
Fractions:
For fractions the weights of the digit positions are written from right of the
binary point and weights are given as follows:
1 2 3 4
1 2 3
2 2 2 2 4
1/2 = 0.5 1/4 = 0.25 1/8 = 0.125 1/16 = 0.0625
E.g.: (0.0110)2 = (……………..)10
=0x2-1 + 1x2-2+ 1x2-3 + 0x2-4
=0 x0.5 + 1x0.25 + 1x0.125 + 0x0.0625
= (0.375)10
E.g .: (1011.101)2 = (…………….)10
= 1x23 + 0x22 + 1x21 + 1x20 + 1x2-1 + 0x2-2 + 1x2-3
= 8 + 0 + 2 + 1 + 0.5 + 0 + 0.125 = (11.625)10

DIGITAL LOGIC FUNDAM ENTALS Page 4


Decimal to binary conversion:
In this method the decimal number is divided by 2 progressively and the
remainder is written after each division. Then the remainders are taken in the
reverse order to form the binary number.
E.g.: (12) 10 = (………) 2
Divisor Dividend Remainder
2 12
2 6 0
2 3 0
2 1 1
0 1
(12) 10 = ( 1100 ) 2
E.g.: (21) 10 = (………)
Divisor Dividend Remainder
2 21
2 10 1
2 5 0
2 2 1
2 1 0
0 1
(21) 10 = ( 10101 )
Fractions:
The fraction is multiplied by 2 and the carry in the integer position is written
after each multiplication. Then they are written in the forward order to get the
corresponding binary equivalent.
E.g.:
Multiply by 2 Result Digit
2 x 0.4375 0.875 0
2 x 0.8750 1.75 1
2 x 0. 750 1.5 1
2 x 0.5 0.5 1

(0.4375) 10 = (0.0111) 2
2.2.2 Octal Number System
Octal number system has a base of 8 i.e., it has eight basic symbols. First eight
decimal digits 0, 1,2,3,4,5,6,7 are used in this system.

DIGITAL LOGIC FUNDAM ENTALS Page 5


Octal to decimal conversion:
In the octal number system each digit corresponds to the powers of 8. The
weight of digital position in octal number is as follows:
83 82 81 80 8-1 8-2 8-3 8-4
To convert from octal to decimal multiply each octal digit by its weight and
add the resulting products.
E.g.:
(47)8 = (……..)10
47 = 4 x 81 + 7 x 80
= 32 + 7
= (39)10
E.g.:
(22.34)8 = (………….)10
22.34 = 2 x 81 + 2 x 80 + 3 x 8-1 + 4 x 8-2
=16 + 2 + 3 x 1/8 +4 x 1/64
= (18.4375)10
Decimal to octal conversion:
Here the number is divided by 8 progressively and each time the remainder is
written and finally the remainders are written in the reverse order to form the
octal number. If the number has a fraction part, that part is multiplied by 8
and carry in the integer part is taken. Finally the carries are taken in the
forward order.
E.g.: (19.11)10 = X8

Multiply by 8 Digit
0.11 x 8 = 0.88 0
0.88 x 8 = 7.04 7
0.04 x 8 = 0.32 0
0.32 x 8 = 2.56 2
0.56 x 8 = 4.48 4
(19.11) 10 = (23.07024) 8
Octal to binary conversion :

Since the base of octal number is 8, i.e., the third power of 2, each octal
number is converted into its equivalent binary digit of length three.

DIGITAL LOGIC FUNDAM ENTALS Page 6


E.g.: (57.127) 8 = (………) 2
5 7 . 1 2 7
101 111 . 001 010 111
(57.127) 8 = (101111.001010111) 2
Binary to octal:
The given binary number is grouped into a group of 3 bits, starting at the
octal point and each group is converted into its octal equivalent.
E.g.:
(1101101.11101)2 = (……………)8
001 101 101. 111 010
1 5 5 . 7 2

(1101101.11101) 2 = (155.72) 8

2.2.3 Hexadecimal Number System:


The hexadecimal number system has a base of 16. It has 16 symbols from 0
through 9 and A through F.

Decimal Hexadecimal Binary


0 0 0000
1 1 0001
2 2 0010
3 3 0011
4 4 0100
5 5 0101
6 6 0110
7 7 0111
8 8 1000
9 9 1001
10 A 1010
11 B 1011
12 C 1100
13 D 1101
14 E 1110
15 F 1111

DIGITAL LOGIC FUNDAM ENTALS Page 7


Binary to hexadecimal:
The binary number is grouped into bits of 4 from the binary point then the
corresponding hexadecimal equivalent is written.
E.g.: (100101110 . 11011) 2 = X16
0001 0010 1110 . 1101 1000
1 2 E . D 8
(100101110 . 11011) 2 = (12E . D8) 16

Hexadecimal to binary:
Since the base of hexadecimal number is 16, i.e., the fourth power of 2, each
hexadecimal number is converted into its equivalent binary digit of length
four.
E.g.: (5D. 2A) 16 = (…….) 2
5 D . 2 A
0101 1101 . 0010 1010

( 5 D. 2 A) 16 = ( 0101 1 10 1. 0 01 0 1 0 1 0 )

Decimal to hexadecimal:
The decimal number is divided by 16 and carries are taken after each division
and then written in the reverse order. The fractional part is multiplied by 16
and carry is taken in the forward order.

E.g.: (2479.859) 10 = (…………....)16


Number Result Remainder
2479 /16 154 15 (F)
154/16 9 10 ( A)
16-Sep 0 9

Remainder Result Number


16 x 0.859 13.744 13 (D)
16 x 0.744 11.904 11 (B)
16 x 0.904 14.464 14 ( E)
16 x 0.464 7.424 7
16 x 0.424 6.784 6

(2479.859) 10 = (9AF.DBE76) 16

DIGITAL LOGIC FUNDAM ENTALS Page 8


Hexadecimal to decimal:
Each digit of the hexadecimal number is multiplied by its weight and then
added.
E.g.: (81.21) 16 = (…………) 10
=8 x 16 1 + 1 x 16 0 + 2 x 16 -1 + 1 x 16 -2
=8 x 16 + 1 x 1 + 2/16 + 1/16 2
= (129.1289) 10
(81.21) 16 = (129.1289) 10
==============================================================
2.2.4 Binary Arithmetic
Binary Addition: To perform the binary addition we have to follow the binary
table given below.
0+0=0
0+1=1
1+0=1
1 + 1 = 0 => plus a carry-over of 1
Carry-overs are performed in the same manner as in decimal arithmetic. Since
1 is the largest digit in the binary system, any sum greater than 1 requires
that a digit be considered over.
10010 11.110
+ 10110 + 101.11
101000 1001 .100
Binary Subtraction:
To perform the binary subtraction the following binary subtraction table
should be followed.
0–0=0
1–0=1
1–1=0
0 – 1 = 1 with a borrow of 1 is equivalent to 10 – 1 = 1
E.g.:
111
-- 010
101

DIGITAL LOGIC FUNDAM ENTALS Page 9


E.g.:
110.01
-- 100.10
001.11
1’s complement:
To obtain 1’s complement of a binary number each bit of the number is
subtracted from 1.
E.g.:
Binary 1’s
number Complem ent

0101 1010
1001 0110
1101 0010
0001 1110
Thus 1’s complement of a binary number is the number that results when we
change each 0 to a 1 and each 1 to a 0.

1’s complement subtraction :


Instead of subtracting the second number from the first, the 1’s complement
of the second number is added to the first number. The last carry which is said
to be a END AROUND CARRY, is added to get the final result.

If there is no carry in the 1’s complement subtraction, it indicates that the


result is a negative and number will be in its 1’s complement form. So
complement it to get the final result.
E.g.:
8 1000 -----------------> 1000 +
-- 10 1010 1’s complement 0101
-2 1101 1’s complement - 0010  result

DIGITAL LOGIC FUNDAM ENTALS Page 10


The following points should be noted down when we do 1’s complement
subtraction.
1. Write the first number (minuend) as such.
2. Write the 1’s complement of second number(subtrahend)
3. Add the two numbers.
4. The carry that arises from the addition is said to be “end around carry”.
5. End-around carry should be added with the sum to get the result.
6. If there is no end around carry find out the 1’s complement of the sum
and put a negative sign before the result as the result is negative.

2’s Complement: 2’s complement results when we add ‘1’ to 1’s complement
of the given number i.e., 2’s complement =1’s complement + 1
Binary Number 1’s comple me nt 2’s comple me nt

1010 0101 0110


0101 1010 1011
1001 0110 0111
0001 1110 1111
2’s Complement Subtraction :
Steps:
1. Write the first number as such
2. Write down the 2’s complement of the second number.
3. Add the two numbers.
4. If there is a carry, discard it and the remaining part (sum) will be the
result (positive).
5. If there is no carry, find out the 2’s complement of the sum and put
negative sign before the result as the result is negative.

DIGITAL LOGIC FUNDAM ENTALS Page 11


Binary multiplication :
The table for binary multiplication is given below
0x 0=0
0x 1=0
1x 0=0
1x 1=1
E.g.:
1011 x 110

1011 x
110
0000
1011
1011 __
1000010_
E.g.:
101.01 x 11.01
101.01 x
11.01

10101
00000
10101
_10101 __
10001.0001
Binary division :
The table for binary division is as follows.
0÷1=0
1÷1=1
As in the decimal system division by zero is meaning less.

E.g:

DIGITAL LOGIC FUNDAM ENTALS Page 12


2.2.5 Alphanumeric code

Computers, printers and the other devices must process both alphabetic and
numeric information. Serial coding systems have been developed to represent
alphanumeric information as a series of 1’s and 0’s. The characters to be coded
are alphabets(26), numerals (10) and special characters such as +,-, /,*, $ etc,
In order to code a character, string of binary digits is used. In order to ensure
uniformity in coding, two standard codes have been used.
1. ASCII: American Standard Code for Information Interchange.

2. Unicode :
ASCII :
 It is a character encoding standard for electronic communication.
American Standard Code for Information Interchange(ASCII) and was
first launched in 1963. ASCII codes are used to represent text in
computers and telecom devices.
 ASCII is used for representing 128 English characters in the form of
numbers, with each letter being assigned to a specific number in the
range 0 to 127. For e.g., the ASCII code for uppercase A is 65,
uppercase B is 66, and so on. Check out the following table for some
more examples.
 Most computers are using ASCII encoding for text representation, which
makes transferring data from one device to another a lot easier.
Unicode :
 is a text encoding standard maintained by the Unicode Consortium
designed to support the use of text written in all of the world's major
writing systems
 Unicode provides a unique way to define every character in every
spoken language of the world by assigning it a unique number. The
Unicode standard is maintained by the Unicode Consortium and
defines more than 1,40,000 characters from more than 150 modern
and historic scripts along with emoji.

DIGITAL LOGIC FUNDAM ENTALS Page 13


2.2.6 Hexadecimal Arithmetic
Hexadecimal Addition
+ 0 1 2 3 4 5 6 7 8 9 A B C D E F
0 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
1 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F 10
2 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F 10 11
3 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F 10 11 12
4 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F 10 11 12 13
5 05 06 07 08 09 0A 0B 0C 0D 0E 0F 10 11 12 13 14
6 06 07 08 09 0A 0B 0C 0D 0E 0F 10 11 12 13 14 15
7 07 08 09 0A 0B 0C 0D 0E 0F 10 11 12 13 14 15 16
8 08 09 0A 0B 0C 0D 0E 0F 10 11 12 13 14 15 16 17
9 09 0A 0B 0C 0D 0E 0F 10 11 12 13 14 15 16 17 18
A 0A 0B 0C 0D 0E 0F 10 11 12 13 14 15 16 17 18 19
B 0B 0C 0D 0E 0F 10 11 12 13 14 15 16 17 18 19 1A
C 0C 0D 0E 0F 10 11 12 13 14 15 16 17 18 19 1A 1B
D 0D 0E 0F 10 11 12 13 14 15 16 17 18 19 1A 1B 1C
E 0E 0F 10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D
F 0F 10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E
Hexadecimal Addition Table
carry 0 00 000
Addend 3B2 3B2 3B2
Augend + 41C + 41C + 41C
Sum E CE 7CE
Carry 1 1 11 11
Addend A27 A27 A27 A27
Augend + C3B + C3B + C3B + C3B
Sum 2 62 662 1 662

Hexadecimal Subtraction
Uses the same principle of "borrowing" that decimal and binary subtraction uses.
borrow
Minuend 6E 6E
Subtrahend - 29 29

Difference 5 45

borrow 1 01 0 01
Minuend AC3 AC3 AC3
Subtrahend - 604 -6 04 -604

Difference F BF 4 B F

DIGITAL LOGIC FUNDAM ENTALS Page 14


2.2.6 Exercise 1 : Choose the correct answer

1. 2 x 101 + 8 x 100 is equal to


(a) 10 (b) 280 (c) 2.8 (d) 28

2. The binary numbe r 1101 is equal to the decimal number


(a) 13 (b) 49 (c) 11 (d) 3

3. The decimal 17 is equal to the binary number


(a) 10010 (b) 11000 (c) 10001 (d) 01001

4. The sum of 11010 + 01111 equals


(a) 101001 (b) 101010 (c) 110101 (d) 101000
5. The difference of 110 – 010 equals
(a) 001 (b) 010 (c) 101 (d) 100
6. The 1’s complement of 10111001 is :
(a) 01000111 (b) 01000110 (c) 11000110 (d) 10101010
7. The 2’s complement of 11001000 is
(a) 00110111 (b) 00110001 (c) 01001000 (d) 00111000
8. The binary number 101100111001010100001 can be written in octal as
(a) 54712308 (b) 54712418 (c) 26345218 (d) 231625018
9. The binary number 10001101010001101111 can be written in hexadecimal as
(a)AD46716 (b) 8C46F16 (c) 8D46F16 (d) AE46F16
10. The BCD number for decimal 473 is
( a) 111011010 (b)110111110101001 (c) 010001110011 (d) 010011110011

DIGITAL LOGIC FUNDAM ENTALS Page 15


2.3 LOGIC GATES AND LOGIC CIRCUITS
2.3.1 AND gate
A gate is simply an electronic circuit which operates a one or more signals to
produce an output signal. The output is high only for certain combination of
input signals.
An AND gate (Figure 2.1) has a high output only when all inputs are high.
The output is low when any one input is low.

Figure 2.1 AND gate


Boolean expression for AND gate operation is Y= A.B
A B Y=A.B
0 0 0
0 1 0
1 0 0
1 1 1

2.3.2 OR gate
An OR gate (Figure 2.2) produces a high output when any or the entire
inputs are high. The output is low only when all the inputs are low.

Figure 2.2 OR gate


The Boolean expression for an OR gate is Y=A+B
Truth table:
A B Y=A+B
0 0 0
0 1 1
1 0 1
1 1 1

DIGITAL LOGIC FUNDAM ENTALS Page 16


2.3.3 NOT gate:
A NOT gate (Figure 2.3) is also called an inverter. The circuit has one input
and one output. The output is the complement of the input. If the input signal
is high, the output is low and vice versa.

Figure 2.3 NOT gate


The Boolean expression for NOT gate is Y=Ā
Truth table:

A Y=Ā

0 1
1 0
If two NOT gates are cascaded then the output will be same as the input and
the circuit is called buffer circuit.

2.3.4 NAND gate


A NAND (Figure 2.4) gate has two or more input signals but only one output
signal. All input signals must be high to get a low output. When one AND
gate is combined with a NOT gate, a NAND gate is obtained.

Figure 2.4 NAND gate


Truth table:

A B Y= AB
0 0 1
0 1 1
1 0 1
1 1 0

DIGITAL LOGIC FUNDAM ENTALS Page 17


2.3.5 NOR gate:
NOR gate (Fig. 2.5) has two or more input signals and one output signal. It
consists of one OR gate followed by an inverter. A NOR gate produces a high
output only when all the inputs are low.

Figure 2.5 NOR gate


Truth table:

A B Y= A B
0 0 1
0 1 0
1 0 0
1 1 0
2.3.6 XOR gate: XOR (Figure 2.6) gate is an abbreviation of exclusive OR gate.
It has two inputs and one output. For a two input XOR gate, the output is
high when the inputs are different and the output is low when the inputs are
same. In general, the output of an XOR gate is high when the number of its
high inputs is odd. The Boolean expression of the XOR gate is Y = A.B’ + A’.B

(a) Logic diagram (b) Logic symbol


Figure 2.6 XOR gate
Truth table:

A B Y= A B
0 0 0
0 1 1
1 0 1
1 1 0

DIGITAL LOGIC FUNDAM ENTALS Page 18


2.3.7 Logic circuits/networks
Logic gates can be combined together to produce more complex logic
circuits (networks). The output from a logic circuit (network) is checked by
producing a truth table.
Key Point : Two different types of problem are considered here:
– drawing the truth table from a given logic circuit (network)
– designing a logic circuit (network) from a given problem and testing it
by also drawing a truth table.
E.g 1 :
Produce a truth table from the following logic circuit (network).

First part
There are 3 inputs; thus we must have 2^3 (i.e. 8) possible combinations of 1s
and 0s. To find the values (outputs) at points P and Q, it is necessary to consider
the truth tables for the NOR gate (output P) and the AND gate (output Q) i.e.
P = A NOR B
Q = B AND C

DIGITAL LOGIC FUNDAM ENTALS Page 19


Second part
There are 8 values from P and Q which form the inputs to the last OR gate.
Hence we get X = P OR Q which gives the following truth table:

Which now gives us the final truth table for the logic circuit given at the
start of the example:

E.g 2 : Consider the following problem.


A system used 3 switches A, B and C; a combination of switches determines
whether an alarm, X, sounds:
If switch A or switch B are in the ON position and if switch C is in the OFF
position then a signal to sound an alarm, X is produced.
It is possible to convert this problem into a logic statement. So we get:
If (A = 1 OR B = 1) AND (C = NOT 1) then X = 1
The first part is two The output from the The third part is inputs (A and B) first
part and the one input (C) which is joined by an OR third part are joined put

DIGITAL LOGIC FUNDAM ENTALS Page 20


through a NOT gate by an AND gate gate So we get the following logic circuit
(network):

This gives the following truth table:

Try to Solve : A manufacturing process is controlled by a built in logic circuit


which is made up of AND, OR and NOT gates only. The process receives a STOP
signal (i.e. X = 1) depending on certain conditions, shown in the following table:

A stop signal (X = 1) occurs when:


either Volume, V > 1000 litres and Speed, S <= 15 m/s
or Temperature, T <= 750ºC and Speed, S > 15 m/s
Draw the logic circuit and truth table to show all the possible situations
when the stop signal could be received.

DIGITAL LOGIC FUNDAM ENTALS Page 21


2.3.8. Exercise 2

1. An inverter performs an operation known as


(a) Complementation (b) assertion
(c) Inversion (d) both answers (a) and (c)

2.The output of gate is LOW when at least one of its inputs is HIGH. It is true
for (a) AND (b) NAND (c) OR (d) NOR

3.The output of gate is HIGH when at least one of its inputs is LOW. It is true
for (a) AND (b) OR (c) NAND (d) NOR

4.The output of a gate is HIGH if and only if all its inputs are HIGH. It is true
for (a)XOR (b) AND (c) OR (d) NAND

5. The output of a gate is LOW if and only if all its inputs are HIGH. It is true
for (a)AND (b) XNOR (c) NOR (d) NAND

6.Which of the following gates cannot be used as an inverter?


(a)NAND (b) AND (c) NOR (d) None of the above

7.The complement of a variable is always


(a) 0 (b) 1 (c) equal to the variable (d) the inverse of the variable

8.Which one of the following is not a valid rule of Boolean algebra?


(a) A + 1 = 1 (b) A = Ā (c) A.A = A (d) A + 0 = A

9. Which of the following rules states that if one input of an AND gate is
always 1 , the output is equal to the other input ?
(a) A + 1 = 1 (b) A + A = A (c) A.A = A (d) A . 1 = A

DIGITAL LOGIC FUNDAM ENTALS Page 22


2.6 Glossary

Alphanumeric : Consisting of numerals, letters, and other characters.


ASCII : American Standard Code for Information Interchange.
BCD : Binary Coded Decimal ; a digit code in which each of the decimal digits ,
0 through 9, is represented by a group of four bits.
Binary : Describes a number system that has a base of two and utilizes 1 and 0
as its digits.
Boolean algebra : The mathematics of logic circuits.
Gate : A logic circuit that performs a specified logic operation , such as AND,
OR or NOT.
Hexadecimal : Describes a number system with a base of 16
Octal : Describes a number system with a base of 8.
Parity : Parity is based on the number of 1’s in a binary word. If the number of
1’s in a word is odd, then it is a odd parity word and if the number of 1’s is
even, then it is an even parity word.
Truth table : A table showing the inputs and corresponding output levels of a
logic circuit.
Universal gate : Either a NAND gate or a NOR gate.

DIGITAL LOGIC FUNDAM ENTALS Page 23


2.7 ANSWERS TO CHECK YOUR PROGRESS QUESTIONS

Check Your Progress 1

1.(d) 2.(a) 3.(c) 4.(a) 5.(d) 6.(b) 7.(d) 8.(b) 9.(c) 10.(c)

Check Your Progress 2

1.(d) 2.(d) 3.(c) 4.(b) 5.(d) 6.(b) 7.(d) 8.(b) 9.(d)

DIGITAL LOGIC FUNDAM ENTALS Page 24


CHAPTER THREE
Problem Solving , Algorithms, and Flowchart

STRUCTURE

3.0 Introduction

3.1 Problem Solving (Definitions)

3.2 Steps for Problem Solving

3.3 Altgorithms

3.3.1 How to write algorithms

3.4 Flowcharts

3.4.1 FlowChart Symbols

3.4.2 Control Structure

3.5 Solved Examples on algorithms and Flowcharts

DIGITAL LOGIC FUNDAM ENTALS Page 25


3.1 Problem Solving (Definitions):
1. Problem solving is the process of identifying a problem, developing an
algorithm for the identified problem and finally implementing the algorithm to
develop a computer program.

2. Problem solving is the process of transforming the description of a problem


into a solution by using our knowledge of the problem domain and by relying on
our ability to select and use appropriate problem-solving strategies, techniques
and tools.

3.2 Steps for Problem Solving:


1. Analysing the problem (Problem Definition):
It is important to clearly understand a problem before we begin to find the
solution for it to figure out proper input and output.

To analyze the problem we must determine its requirements:


a. Does the program require user interaction?
b. Does the program manipulate data?
c. What is the output?
d. Are all possible circumstances handled?
e. Is the problem complex? if it is divide it into subproblems
f. Analyze each subproblem as above

2. Developing (Design) an Algorithm : It is essential to device a solution


before writing a program code for a given problem. The solution is represented in
natural language and is called an algorithm. First we produce a general algorithm
(one can use pseudocode) and refine the algorithm successively to get step by
step detailed algorithm that is very close to a computer language.

Pseudocode: is an artificial and informal language that helps programmers


develop algorithms. Pseudocode is very similar to everyday English.

DIGITAL LOGIC FUNDAM ENTALS Page 26


Algorithm: it is defined as sequence of steps to solve a problem (task). The
steps must be finite, well defined and unambiguous. Writing algorithm requires
some thinking. Algorithm can also be defined as a plan to solve a problem and
represents its logic. Note that an algorithm is of no use if it does not help us
arrive at the desired solution

Flowchart: it is a pictorial (graphical) representation of an algorithm. A


flowchart is drawn using different kinds of symbols. A symbol is used for a
specific purpose. Each symbol has name. Here's a comparison between Algorithm,
Flowchart, and a program
Algorithm Flowchart Program
An algorithm is defined as A flowchart is pictorial Set of instructions.
sequence of steps to solve a (graphical) representation Instruction is a command to
problem (task). of an algorithm. the computer to do some
task.
Algorithm can also be defined A picture is worth of 1000 Implementation of
as a plan to solve a problem words. We can understand Algorithm or flowchart
and represents its logic. more from picture

3. Writing a Program (Coding): After finalising the algorithm, we need to


convert the algorithm into the format which can be understood by the computer
to generate the desired solution by different high level programming languages.
After testing an algorithm, we can code it in any programming languaged, like C,
C++, JAVA, and others.
After writing the code, the computer must do the following:
- Compilation - Execution:
- Compilation is performed by a program called compiler that do the following:
1. Translates the source code into object code (machine code)
2. Checks for syntax errors and warnings.
3. Saves the object code to a disk file
4. If any compiler errors are received, no object code file will be generated.
5. An object code file will be generated if only warnings, not errors, are received.

DIGITAL LOGIC FUNDAM ENTALS Page 27


Execution: when the program does not have errors, the computer executes it to
produce the output.

4. Testing and Debugging: The program created should be tested on various


parameters. The program should meet the requirements of the user. It must
respond within the expected time. It should generate correct output for all
possible inputs.

There are three types of errors that can be arised during the phases of problem
solving:

1. Syntax errors: are detected by the compiler


- Source code does not conform to one or more of the language grammar rules
- Example of syntax errors: undeclared variable, …
- Often one mistake leads to multiple error messages– which can be confusing

2. Run-time errors: detected and displayed by the computer during execution.


- Occurs when a program directs a computer to perform an illegal operation.
Example: int x=y/0;
- will stop program execution and display a message.

3. Logic errors: caused by faulty algorithm


- sign of error: incorrect program output
- cure: thorough testing and comparison with expected results
A logic error is referred to as a bug, so finding logic errors is called debugging .

DIGITAL LOGIC FUNDAM ENTALS Page 28


3.3 Algorithms
The word “algorithm” relates to the name of the mathematician Al-khowarizmi,
which means a procedure or a technique. Software Engineer commonly uses an
algorithm for planning and solving the problems. An algorithm is a sequence of
steps to solve a particular problem or algorithm is an ordered set of unambiguous
steps that produces a result and terminates in a finite time.
Algorithm has the following characteristics
1. It should have finite number of steps.
2. The steps must be in order and simple.
3. Each step should be defined clearly i.e. without un-ambiguity
4. Must include all required information
5. Should exhibit at least one output

3.3.1 HOW TO WRITE ALGORITHMS


Step 1 : Define your algorithms input: Many algorithms take in data to be
processed, e.g. to calculate the area of rectangle input may be the
rectangle height and rectangle width.

Step 2 : Define the variables: Algorithm's variables allow you to use it for more
than one place. We can define two variables for rectangle height and
rectangle width as HEIGHT and WIDTH (or H & W). We should use
meaningful variable name e.g. instead of using H & W use HEIGHT
and WIDTH as variable name.
Step 3 : Outline the algorithm's operations: Use input variable for computation
purpose, e.g. to find area of rectangle multiply the HEIGHT and
WIDTH variable and store the value in new variable (say) AREA. An
algorithm's operations can take the form of multiple steps and even
branch, depending on the value of the input variables.
Step 4 : Output the results of your algorithm's operations: In case of area of
rectangle output will be the value stored in variable AREA. if the
input variables described a rectangle with a HEIGHT of 2 and a
WIDTH of 3, the algorithm would output the value of 6.

DIGITAL LOGIC FUNDAM ENTALS Page 29


3.4 Flowchart
A flow chart, or flow diagram, is a graphical representation of a process or system
that details the sequencing of steps required to create output. A typical flowchart
uses a set of basic symbols to represent various functions, and shows the
sequence and interconnection of functions with lines and arrows.

Flow charts can be used to document virtually any type of business system, from
the movement of materials through machinery in a manufacturing operation to
the flow of applicant information through the hiring process in a human
resources department.

Each flow chart is concerned with one particular process or system. It begins with
the input of data or materials into the system and traces all the procedures
needed to convert the input into its final output form.

Specialized flow chart symbols show the processes that take place, the actions
that are performed in each step, and the relationship between various steps.

Flow charts can include different levels of detail as needed, from a highlevel
overview of an entire system to a detailed diagram of one component process
ithin a larger system. In any case, the flow chart shows the overall structure of
the process or system, traces the flow of information and work through it, and
highlights key processing and decision points.

3.4.1 FlowChart Symbols


 Terminal symbol - indicates the beginning
and end points of an algorithm.

 Process symbol - shows an instruction


other than input, output or selection.

 Input-output symbol - shows an input or an output operation.

DIGITAL LOGIC FUNDAM ENTALS Page 30


 Disk storage I/O symbol - indicates input
from or output to disk storage.

 Printer output symbol - shows hardcopy


Printer output.

 Selection symbol - shows a selection process


for two-way selection.

 Off-page connector - provides continuation of a


logical path on another page.

 On-page connector - provides continuation


of logical path at another point in the same page.

 Flow lines - indicate the logical sequence of


execution steps in the algorithm.

 Module: The position of the module symbol


Indicates the point the module is executed.
3.4.2 Control structures
The algorithm and flowchart include following three types of control structures :
1. Sequence: In the sequence structure, statements are placed one after the
other and the execution takes place starting from up to down.
The beginning and end of a block of statements can be optionally marked with
the keywords beginand end.

Format : Begin
statement 1.
statement 2.

statement n.
End

DIGITAL LOGIC FUNDAM ENTALS Page 31


Ex1 : calculate a person’s age
Algorithm Flowchart

Begin

read birth year

age = current year –birth year

display age

End

2. Branching (Selection): In branch control, there is a condition and according


to a condition, a decision of either TRUE or FALSE is achieved.

In the case of TRUE, one of the two branches is explored; but in the case of
FALSE condition, the other alternative is taken. Generally, the ‘IF-THEN’ is used to
represent branch control.

Format:

if (condition)

then-part

else

else-part

end_if
EX 2: Determine if a man is retired
Begin

DIGITAL LOGIC FUNDAM ENTALS Page 32


read age
if (age is greater than 55)
print “Retired”
else
print “Still working”
end_if
End

Begin
read age
if (age > 55)
print “Retired”
else
print “Still working”
end_if
End
 Sometimes in certain situation, we may omit the else-part.

if (number is odd number)


print “This is an odd number”
end_if
 Nested selection structure: basic selection structure that contains other if/else
structure in its then-part or else-part.

if (number is equal to 1)
print “One”
else if (number is equal to 2)
print “Two”
else if (number is equal to 3)
print “Three”
else
print “Other”
end_if

DIGITAL LOGIC FUNDAM ENTALS Page 33


3. Loop (Repetition): The Loop or Repetition allows a statement(s) to be
executed repeatedly based on certain loop condition e.g. WHILE, FOR loops .
Format :

while (condition)
loop-body
end_while

Ex 3 : Write a program that reads and displays the age of 10 people (one after
another).
Note:
For this problem, we need away to count how many people whose age have been
processed (read and displayed). Therefore, we introduce a concept of counter, a
variable used to count the number of people whose age have been processed by
the program.
1. Algorithm

DIGITAL LOGIC FUNDAM ENTALS Page 34


2. Flowchart

Ex 4 :
Write a n algorithm that will calculate and print the age of 10 persons, given
their birth year. If the age of the person is above 55, then the program will print
“Retired”, otherwise, the program will print “Still working”.

DIGITAL LOGIC FUNDAM ENTALS Page 35


3.5 Solved Examples on algorithms and Flowcharts
Ex1:
Create a program to compute the volume of a sphere. Use the formula:
V = (4/3) *pi*r 3 where pi is equal to 3.1416 approximately. The r is the radius of
sphere. Display the result.

Ex 2 : Write a program the converts the input Celsius degree into its equivalent
Fahrenheit degree. Use the formula: F = (9/5) *C+32.

DIGITAL LOGIC FUNDAM ENTALS Page 36


Ex 3: Write a program that converts the input dollar to its peso exchange rate
equivalent. Assume that the present exchange rate is 51.50 pesos against the
dollar. Then display the peso equivalent exchange rate.

Ex 4: Write a program that converts an input inch(es) into its equivalent


centimeters. Take note that one inch is equivalent to 2.54cms

DIGITAL LOGIC FUNDAM ENTALS Page 37


Ex5 : Write a program that exchanges the value of two variables: x and y. The
output must be: the value of variable y will become the value of variable x, and
vice versa.

Ex 6 : Design a program to find the circumference of a circle. Use the formula:


C=2πr, where π is approximately equivalent 3.1416.

DIGITAL LOGIC FUNDAM ENTALS Page 38


Ex 7: Write a program that takes as input the purchase price of an item (P), its
expected number of years of service (Y) and its expected salvage value (S). Then
outputs the yearly depreciation for the item (D). Use the formula: D = (P - S) Y.

Ex8 : Swapping of 2 variables without using temporary (or 3rd variable)

DIGITAL LOGIC FUNDAM ENTALS Page 39


Ex 9 : Determine the most economical quantity to be stocked for each product
that a manufacturing company has in its inventory: This quantity, called
economic order quantity (EOQ) is calculated as follows: EOQ=2rs/1 where: R=
total yearly production requirement S=set up cost per order I=inventory carrying
cost per unit.

E10 : Write a program to compute the radius of a circle. Derive your formula from
the given equation: A=πr², then display the output.

DIGITAL LOGIC FUNDAM ENTALS Page 40

You might also like