Informatics College Pokhara: Fundamentals of Computing CS4051NP
Informatics College Pokhara: Fundamentals of Computing CS4051NP
Fundamentals of Computing
CS4051NP
Coursework I
1. Introduction ............................................................................................................................... 1
2. Model ......................................................................................................................................... 3
3. Algorithm ................................................................................................................................. 14
5. Pseudocode .............................................................................................................................. 17
7. Testing...................................................................................................................................... 24
8. Conclusion ............................................................................................................................... 31
9. References................................................................................................................................ 32
TABLE OF FIGURES
1. INTRODUCTION
Python is a general-purpose, interpreted high-level programming language that allows programmers to
use various programming outlets to create easy or difficult programs, get quicker outputs and is designed
to read at ease and simple to implement (Janssen, 2019). One can use Python for developing complex
scientific and numeric applications. Similarly, as we were learning the methodology of Python, an
individual coursework for this module was delivered to students that demands organizing a software
application for a digital circuit to calculate integer addition. This report of mine provides all the important
mechanisms of the model, algorithm, data structures, program and testing as well.
It is all necessity to build programs in Python that carry out the system of data and inferences of the
adder we developed, then test the program accordingly. Besides, the project integrates the manufacture
of a model of a byte adder forgathered with the help of electronic gates such as XOR Gate, OR Gate &
AND Gate, predicated through bit adder by prescribing an algorithm for addition of integer based on
bitwise operations, after that, choosing favorable python data structures to categorize the data or findings
that is to be processed by the program. And before the software application was developed with the
model, I drew flowchart diagram and then wrote algorithm in a stepwise manner specified with
pseudocode in order to make coding better understandable and work effective. After that, I created a
program implementing the model of the binary adder constructed in the beginning. The program
performs an operation in a circular strip that is loop, scanning user provided two integer digits computing
the total sum of the numbers then prints the result according to the instructions in binary or decimal.
I ensured a full adder to give practical effect using one OR gate and two half adders since its simple to
carry out half adder. In full adder and half adder, I have applied K-Map simplification to prevail the
mathematical expression for the output of Sum and Carry. The only data structure that has been used in
the program is ‘List’ for instance; conversion of integer number to binary list. The program that I have
formed is constructed with standardized units in a modular way with individual functions for inputting
data, integer operations, bit operations, and following the output result of the whole mathematical
operations to find a value. The adder model is well explained through better illustrations. To add, I have
used draw.io and lucid chart for the construction of circuit diagrams.
The flexibility of this individual coursework of Fundamentals of Computing leads to develop our skills
and knowledge even more on python and mostly the function of byte adder. It is important to meet all
the required aspects as mentioned in the question module, apart from that we get to be more familiar
with modular programming and learn about digital logic circuits as well.
Avash Joshi 1
CS4051NP Fundamentals of Computing
The important goal of this project is the development of a software application that propels to growth
or to greater activity of a digital circuit executing the process of adding integers.
A state or condition permitting clear perception or understanding of the model construction of a byte
adder using logical electronic gates based on the model of bit adder.
Including a program, a documentation that clearly explains all the required model, algorithm, data
structures.
To learn more about the model of bit adder to form byte adder represented through several diagrams.
Clear visions of the algorithm for adding two integers specified with pseudocodes and flowchart as
well.
Testing program with proper test cases, and presented with proper testing format so that the program
works absolutely smooth without any errors.
The enhancement in the skills/knowledge about syntax, variables, decision making statements, data
types, loops, data structures and digital logic circuits.
Avash Joshi 2
CS4051NP Fundamentals of Computing
2. MODEL
The bit adder circuit of the coursework is given below which is implemented by the logical gates
which are NAND, NOR and NOT. The model of a byte adder is constructed based on the given bit
adder model which generates sum and carry. With the help of bit adder, I created a full adder model
circuit and from that the required output can be initialized.
C C
Carry to
next
column
Carry to previous
column
C
Sum
Figure 1: Bit Adder Circuit Model
Adder
An adder is a digital circuit that performs addition of numbers. It is a type of calculator that is used to
add two binary numbers. When I say, calculator that means, this one is a circuit that can be assimilated
with numerous other circuits for a wide range of applications. In many computers and other types of
processors, adders are used to calculate addresses, similar operations and table indices in the ALU and
also in other parts of the processors. There are two kinds of adders classified;
i. Half adder
The relationship between the Full-Adder and the Half-Adder is half adder produces results and full adder
uses half adder to produce some other result. Similarly, while the Full-Adder is of two Half-Adders, the
Full-Adder is the actual block that we use to create the arithmetic circuits.
(Techopedia, 2017)
Sum = A ⊕ B
A Sum
Half
Adder
B Adder Carry
Figure 2: Single Bit Half
Adder
The common representation uses a XOR logic gate and an AND logic gate. Lets have a look at the
implementation of half adder circuit below:
Avash Joshi 4
CS4051NP Fundamentals of Computing
A
Sum
B
C
XOR C
Carry
AND C
For complex addition, there may be cases when we have to add two 8 -bit bytes together. And this is
possible only with the help of full-adder logic.
With the help of half adder, we can design circuits that are capable of performing simple addition with
the help of logic gates. Let us first take a look at the addition of single bits.
0+0=0
0+1=1
1+0=1
1 + 1 = 10
These are the slightest conceivable single-bit combinations. But the result for 1+1 is 10. In spite of the
fact that this issue can be illuminated with the assistance of an EXOR Gate, the sum result must be re-
written as a 2-bit output. Thus the above equations can be written as
0 + 0 = 00
0 + 1 = 01
Avash Joshi 5
CS4051NP Fundamentals of Computing
1 + 0 = 01
1 + 1 = 10
Here the output ‘1’of ‘10’ becomes the carry-out. The result is shown in a truth-table below. ‘SUM’ is
the normal output and ‘CARRY’ is the carry-out.
INPUTS OUTPUT
A B Sum Carry
0 0 0 0
0 1 1 0
1 0 1 0
1 1 0 1
Now, it is clear that this 1-bit adder can be easily implemented with the help of EXOR Gate for the output
‘SUM’ and an AND Gate for the Carry. When we require to add, two 8-bit bytes collectively, we can be
done with the assistance of a full-adder logic. The half-adder is valuable once you need to add one binary
digit amounts. A way to create a two-binary digit adders would be to forma truth table and diminish it.
When you need to form a three binary digit adder, do it once more. When you choose to create a four
digit adder, do it once more. The circuits would be quick, but improvement time is moderate. (Elprocus,
2013)
Avash Joshi 6
CS4051NP Fundamentals of Computing
Sum = A ⊕ B ⊕ Cin
A Cout
B Full Adder
Sum
Cin
Figure 4: Single Bit Full Adder
With this sort of symbol, we can add two bits mutually taking a carry from another lower order of
magnitude, and transferring a carry to the next higher order of magnitude. In a computer, for a multi-bit
operation, each bit must be defined by a full adder and must be added at the same time. Therefore, to add
two 8-bit numbers, we will need 8 full adders that can be created by cascading two of the 4-bit blocks.
The addition of two 4-bit numbers is given below:
Avash Joshi 7
CS4051NP Fundamentals of Computing
A7 B7 A6 B6 A5 B5 A4 B4 A3 B3 A2 B2 A1 B1 A0 B0
C7 S7 C6 S6 C5 S5 C4 S4 C3 S3 C2 S2 C1 S1 C0 S0
Avash Joshi 8
CS4051NP Fundamentals of Computing
INPUTS OUTPUT
A B Cin Cin S
0 0 0 0 0
0 0 1 0 1
0 1 0 0 1
0 1 1 1 0
1 0 0 0 1
1 0 1 1 0
1 1 0 1 0
1 1 1 1 1
Table 2: Truth Table of Full Adder
With the truth-table, the full adder logic can be implemented. The output S is an XOR between the input
A and the half-adder, SUM output with B and Cin inputs. We take Cout which will only be true if any of
the two inputs out of the three are HIGH.
Thus, we can design a full adder circuit with the help of two half adder circuits. At first, half adder will
be used to add A and B to create a partial Sum and a second half adder logic can be used to add C in to
the Sum created by the first half adder to obtain the final S output.
Avash Joshi 9
CS4051NP Fundamentals of Computing
XOR
XOR
AND
OR
AND
Avash Joshi 10
CS4051NP Fundamentals of Computing
Avash Joshi 11
CS4051NP Fundamentals of Computing
Logic gates are the basic building blocks of any digital system. It is an electronic circuit having one or
many input and only one output. The relationship between the input and the output is based on a
certain logic. Based on this, logic gates are termed as AND gate, OR gate, NOT gate and so on. The
three logic gates used in the byte adder circuit are described below:
XOR Gate
The output of XOR Gate is high (1) only when one input is high and is low when both of the inputs
are either low (0) or high (1. XOR Gate is used to perform the calculation of sum in the half adders.
INPUTS OUTPUT
A B A XOR B
0 0 0
0 1 1
1 0 1
1 1 0
Avash Joshi 12
CS4051NP Fundamentals of Computing
AND Gate
The output of AND Gate is high (1) only when all the input is high. AND Gate is used to
calculate Carry Output in both the half adders.
Output = A AND B = A.B
INPUTS OUTPUT
A B A AND B
0 0 0
0 1 0
1 0 0
1 1 1
OR Gate
The output of OR Gate is high (1) when one of the input is high and it is low only when both
the inputs are low (0). OR Gate is used to calculate the final Carry from two carry bit obtained
by two half adders.
Output = A OR B = A + B
Avash Joshi 13
CS4051NP Fundamentals of Computing
INPUTS OUTPUT
A B A OR B
0 0 0
0 1 1
1 0 1
1 1 1
3. ALGORITHM
An algorithm is a set of well-defined instructions or data processing in sequence to solve a problem.
Algorithms can be broadly used in different places like computer programming, mathematics and
everyday lives. It is an arrangement of steps to be taken out for a required output that comes after the
algorithm completed the full process from a certain given input. (Freeman, 2019)
Step 1: Start
Step 2: Read first integer value within the range of 0 to 255 and store it.
Step 3: Read second integer value within the range of 0 to 255 and store it.
Step 4: Convert both the integer values into binary and convert the binary numbers to a list of single-
digit bits.
Step 5: Make both the Binary bit list (binary number) of length 8.
Step 6: Initialize Carry in to 0 for the first iteration (for least significant column).
Step 7: Initialize Final Sum to both Binary number and Decimal number and store the value.
Step 8: Load input in the conversion function and then start over from step 1. Keep on the calculation
until the user willing to perform.
Avash Joshi 14
CS4051NP Fundamentals of Computing
Step 9: Join the Final Result list to form a single Binary number and store in another variable Final Sum.
4. FLOW CHART
A flowchart is a formalized graphic representation of a logic sequence, work or manufacturing process
that explains a process or operation. It incorporates numerous steps, which the process "flows" through
from beginning to ending. Common uses for flowcharts include creating trade plans, characterizing in
defining troubleshooting steps and planning numerical algorithms. Some flowcharts may only include a
couple of steps whereas others can be exceedingly complex, containing hundreds of possible outcomes.
Flowcharts basically use standard symbols to speak to diverse stages or activities inside the chart. For
example, each step is appeared within a rectangle whereas each choice is shown in a diamond. Arrows
are set between the various symbols to display the direction, the process is flowing. (Tech Terms, 2008)
So, the flowchart was created using draw.io and is attached below:-
Avash Joshi 15
CS4051NP Fundamentals of Computing
Avash Joshi 16
CS4051NP Fundamentals of Computing
5. PSEUDOCODE
Pseudocode can be described as an execution of an algorithm within the frame of annotations and
informative content composed in a plain English. It has no sentence structure like every of the
programming dialect and in this way can’t be compiled or translated by the computer. It is a technique
that permits the software engineer to represent the usage of an algorithm. It allows designers or lead
software engineers to precise the plan in extraordinary detail and gives software engineers a nitty-gritty
format for the following step of composing code in a particular programming language.
‘UserInput’ module
while True
try
ask user to input first number value1 after the message “Enter First Integer Number:”
except
continue
else
print message "Error! Please enter an integer value within the range of 0 to 255."
continue
break
Avash Joshi 17
CS4051NP Fundamentals of Computing
while True
try
ask user to input second number value2 after the text “Enter second Integer Number:”
except
continue
else
print message "Error! Please enter an integer value within the range of 0 to 255."
continue
break
End function
Binary module
Initialize upBinList = []
Initialize lowBinList = []
Avash Joshi 18
CS4051NP Fundamentals of Computing
value1 = value1 // 2
while length(List1) != 8:
while length(List2) != 8:
define Decimal(Binary):
set n = 1
set ans_ = 0
Avash Joshi 19
CS4051NP Fundamentals of Computing
R = Binary modulus 10
ans_ = ans_ + n * R
n=n*2
Binary = Binary//10
return ans_
End function
‘Adder’ module
importing Binary
return upper_binary^lower_binary
s_ = _xor_(upper_binary, lower_binary)
c = _and_(upper_binary, lower_binary)
return s_, c
Avash Joshi 20
CS4051NP Fundamentals of Computing
c_ = _or_(firstCarry, secCarry)
return secSum, c_
result = []
result.insert(0, s_)
result.insert(0, carry_in)
result = int("".join(result))
Avash Joshi 21
CS4051NP Fundamentals of Computing
‘Main’ module
while ans is y
importlib.reload(UserInput)
importlib.reload(Binary)
importlib.reload(Adder)
If answer = no
Avash Joshi 22
CS4051NP Fundamentals of Computing
6. DATA STRUCTURE
Data structure is a means of organizing and storing information in order to be accessed and worked with
efficiently. They explain the relationship between the data, and the operations that can be executed on
the data. (Jaiswal, 2017) There are various types of data structures available. The built-ins data structures
are: lists, tuples, dictionaries, strings, sets and frozen sets.
Lists and tuples are ordered sequences of objects. List and tuples can have any sort of objects. Lists and
tuples are like arrays. Lists are mutable hence they can be amplified or decreased at will. Sets are mutable
unordered arrangement of unique components.
Lists are enclosed in brackets:
l = [1, 2, "x"]
Tuples are enclosed in parentheses:
t = (1, 2, "x")
Tuples are faster and consume less memory.
From these data structures, only List has been applied in my program and its uses are as follows:-
Lists are great to use when we want to work with many related values. So, in my program, List
helped me to keep data together that belongs alongside, condense my code, and performed the
same methods and operations on multiple values at once.
The list is a flexible data type and has been modified throughout the course of my program. This
program covered the basic features of lists, including indexing, slicing, modifying, and
concatenating lists.
I have applied list for storing input given by the user and to add “0” in order to make it 8-bits. I
have declared few empty lists in order to store data afterwards when necessary.
Avash Joshi 23
CS4051NP Fundamentals of Computing
During the conversion of binary and decimal numbers into list of single digit binary bit. Insert
function was used to insert the remainder received while dividing the input integer value.
7. TESTING
Testing is the first important thing for formulating a program where a programmer does not get much
clear thought around in case their program is running as expected or not, until the code is tested
correctly. A programmer ought to carry many tests counting compilation of the code, usefulness of
traits, special case dealing with, and many more so that to begin with of all, the program does not halt
while running, and the program runs as per the desires.
7.1. TEST I
Avash Joshi 24
CS4051NP Fundamentals of Computing
Objective: To check if the program works when two valid integer numbers
between 0 and 255 are entered.
Expected Output The program should run without any error and sum of the two integer
numbers should be printed in both binary and decimal form.
Actual Output The sum of the two numbers was displayed in both binary and decimal
values without any error.
7.2. TEST II
Avash Joshi 25
CS4051NP Fundamentals of Computing
Actual Output An error occurred and asked user to enter the value within the provided
range.
Objective: To check if the program runs and displays the sum when the both
integers are minimum values ‘0’
Actual Output The sum of give values was displayed in binary as well as decimal form.
Avash Joshi 26
CS4051NP Fundamentals of Computing
7.4. TEST IV
Objective: To check if the program runs or not until the user wants to perform
calculation.
Action After input of two valid numbers, enter ‘y’ for the continuation of the
program and to stop the program enter ‘n’.
Expected Output User should be able to perform numerous calculation and the programs
doesn’t ask user for the input after entering ‘n’.
Actual Output No any errors occurred and the user was able to perform multiple
calculations.
Avash Joshi 27
CS4051NP Fundamentals of Computing
7.5. TEST V
Objective: To check if the program gives output when input is negative integer or
an integer greater than 255.
Action Input negative integer and next time input integer greater than 255.
Avash Joshi 28
CS4051NP Fundamentals of Computing
7.6. TEST VI
Avash Joshi 29
CS4051NP Fundamentals of Computing
Objective: To check if the binary and decimal outputs are either right or wrong.
Action Check the result of both binary and decimal values with the help of online
calculations.
Expected Output The output values displayed on the screen through coding should be
accurate.
Actual Output Output results were correct and the program works flawlessly.
Avash Joshi 30
CS4051NP Fundamentals of Computing
8. CONCLUSION
Through some relevant researches, online surfing, video tutorial, and practices, I have performed all the
task required in this coursework with all my dedication and as a result, the testimony of documentation
has been furnished as well as the program is working flawlessly and performs the necessary operations
for calculating binary addition. Internet helped me a lot for compact idea to find out the best information
related to the python and byte adder assembled using electronic gates based on the model of the bit adder.
Proper exception handling has been done and code is properly structured in a modular manner. The
program has been well tested with 6 proper test cases, and presented with proper testing format. The
algorithm has been well defined, flowchart and pseudocode are also specifically completed which are
attached within this report. I have constructed a model of a byte adder using logical electronic gates
while being based on the bit adder model given in the coursework. The data structures and digital circuits
have been described nicely with proper examples. And now, I have become more familiar with modular
programming, digital logic circuits, syntax, variables, data types, loops, decision making statements and
and working mechanism of iteration related to this coursework of Python.
In the beginning, I was little bit confused about where to start my coding from. The digital logic circuits,
variables, data structures, loops, decision making statements and more that we have to use were all
unfamiliar to me. I faced problems and errors during the time of my assignment. It was not simple to
analyze where and how to handle exceptions as well as the testing part. Therefore books, seniors and
mostly consoling module teacher, their guidelines was the best idea and help to have better understanding
about the subject matter. It was a great privilege and honor to work and study under their guidance. I am
very much grateful for what they have offered me and it was really encouraging.
To conclude, I am really happy that I have accomplished this project related to python within the given
timeframe. Overall, I am more cleared now about the working mechanism of a byte adder and the
components related to it. The coursework has offered me to figure it out that we need to be working out
more harder and strive for more endless times to perform better in our programming skills/abilities and
steady aptitudes to fulfill our goals.
Avash Joshi 31
CS4051NP Fundamentals of Computing
9. REFERENCES
Elprocus, 2013. Explain Half Adder and Full Adder with Truth Table. [Online]
Available at: https://www.elprocus.com/half-adder-and-full-
adder/#:~:text=An%20adder%20is%20a%20digital%20circuit%20that%20performs%20addition%20o
f,both%20inputs%20to%20produce%20carry.
[Accessed 5 June 2020].
Avash Joshi 32
CS4051NP Fundamentals of Computing
Avash Joshi 33