0% found this document useful (0 votes)
6 views21 pages

Understand Computer Arithmetic and Logic

This document outlines Learning Outcome 5, focusing on understanding computer arithmetic and logic, including various number systems (decimal, binary, octal, hexadecimal) and their representations. It explains integer and floating-point arithmetic according to IEEE standards, including operations like addition, subtraction, multiplication, and division. Additionally, it covers logic operators, operations, and methods for representing these operations in computing.

Uploaded by

marvineotwoma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views21 pages

Understand Computer Arithmetic and Logic

This document outlines Learning Outcome 5, focusing on understanding computer arithmetic and logic, including various number systems (decimal, binary, octal, hexadecimal) and their representations. It explains integer and floating-point arithmetic according to IEEE standards, including operations like addition, subtraction, multiplication, and division. Additionally, it covers logic operators, operations, and methods for representing these operations in computing.

Uploaded by

marvineotwoma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Learning Outcome 5: Understand computer arithmetic and logic

Learning Activities Special Instructions

5.1 Number systems are explained Activities may be carried out in


5.2 Integer and Floating point representations are groups or as individual
demonstrated according to IEEE standard
5.3 Integer and Floating point arithmetic is explained
5.4 Logic operators are explained
5.5 Logic operations are explained
5.6 Methods of representing logic operations are
demonstrated

[Link] Information Sheet No. 2/LO5: Understand computer arithmetic and logic

Introduction
This learning outcome involves explaining number systems, demonstrating Integer and floating
point representations according to IEEE standard, explaining integer and floating point arithmetic,
logic operators and logic operations and demonstrating methods of representing logic operations.

The Number System


The technique to represent and work with numbers is called the number system.
We have different types of numbering systems. They include:
● Decimal number system
● The binary number system
● octal number system
● hexadecimal number system

Decimal Number System


Decimal number system is a base 10 number system having 10 digits from 0 to 9. This means that
any numerical quantity can be represented using these 10 digits. Decimal number system is also a
positional value system. This means that the value of digits will depend on its position. Let us
take an example to understand this.

Say we have three numbers – 734, 971 and 207. The value of 7 in all three numbers is different−
● In 734, value of 7 is 7 hundreds or 700 or 7 × 100 or 7 × 102
● In 971, value of 7 is 7 tens or 70 or 7 × 10 or 7 × 101
● In 207, value 0f 7 is 7 units or 7 or 7 × 1 or 7 × 100

The weightage of each position can be represented as follows –


Decimal Number System

49
Binary Number System
The easiest way to vary instructions through electric signals is the two-state system – on and off.
On is represented as 1 and off as 0, though 0 is not actually a no signal but signal at a lower
voltage.
The number system having just these two digits – 0 and 1 – is called binary number system.
Each binary digit is also called a bit. Binary number system is also a positional value system,
where each digit has a value expressed in powers of 2, as displayed here.

Binary Number System

In any binary number, the rightmost digit is called least significant bit (LSB) and leftmost digit is
called most significant bit (MSB).

Least significant bit (LSB) and most significant bit (MSB)

And the decimal equivalent of this number is the sum of the product of each digit with its
positional value.
110102 = 1×24 + 1×23 + 0×22 + 1×21 + 0×20
= 16 + 8 + 0 + 2 + 0
= 2610

Computer memory is measured in terms of how many bits it can store. Here is a chart for memory
capacity conversion.
● 1 byte (B) = 8 bits
● 1 Kilobytes (KB) = 1024 bytes
● 1 Megabyte (MB) = 1024 KB
● 1 Gigabyte (GB) = 1024 MB
● 1 Terabyte (TB) = 1024 GB
● 1 Exabyte (EB) = 1024 PB
● 1 Zettabyte = 1024 EB
● 1 Yottabyte (YB) = 1024 ZB

50
Octal Number System
Octal number system has eight digits – 0, 1, 2, 3, 4, 5, 6 and 7.
Octal number system is also a positional value system with where each digit has its value
expressed in powers of 8, as shown here –

Octal Number System

Decimal equivalent of any octal number is the sum of the product of each digit with its positional
value.
7268 = 7×82 + 2×81 + 6×80
= 448 + 16 + 6
= 47010

Hexadecimal Number System


Octal number system has 16 symbols – 0 to 9 and A to F where A is equal to 10, B is equal to 11
and so on till F. Hexadecimal number system is also a positional value system with where each
digit has its value expressed in powers of 16, as shown here –

Hexadecimal Number System

Decimal equivalent of any hexadecimal number is the sum of the product of each digit with its
positional value.
27FB16 = 2×163 + 7×162 + 15×161 + 10×160
= 8192 + 1792 + 240 +10
= 1023410

Number System Relationship


The following table depicts the relationship between decimal, binary, octal and hexadecimal
number systems.

Table 8: Number System Relationship

HEXADECIMAL DECIMAL OCTAL BINARY

0 0 0 0000

1 1 1 0001

51
2 2 2 0010

3 3 3 0011

A 10 12 1010

B 11 13 1011

C 12 14 1100

ASCII
Besides numerical data, computers must be able to handle alphabets, punctuation marks,
mathematical operators, special symbols, etc. that form the complete character set of English
language. The complete set of characters or symbols are called alphanumeric codes. The complete
alphanumeric code typically includes –

● 26 upper case letters


● 26 lower case letters
● 10 digits
● 7 punctuation marks
● 20 to 40 special characters

Now a computer understands only numeric values, whatever the number system used. So all
characters must have a numeric equivalent called the alphanumeric code. The most widely used
alphanumeric code is American Standard Code for Information Interchange (ASCII). ASCII is a 7-
bit code that has 128 (27) possible codes.

52
Table 9: ASCII

Unicode
Unicode is an international coding system designed to be used with different language scripts.
Each character or symbol is assigned a unique numeric value, largely within the framework of
ASCII. Earlier, each script had its own encoding system, which could conflict with each other.

In contrast, this is what Unicode officially aims to do − Unicode provides a unique number for
every character, no matter what the platform, no matter what the program, no matter what the
language.

Number Systems Conversions

Decimal to Binary
Decimal numbers can be converted to binary by repeated division of the number by 2 while
recording the remainder.

Example:

53
The remainders are to be read from bottom to top to obtain the binary equivalent.
4310 = 1010112

Decimal to Octal
Decimal numbers can be converted to octal by repeated division of the number by 8 while
recording the remainder.

Example:

Reading the remainders from bottom to top,


47310 = 7318

Decimal to Hexadecimal
Decimal numbers can be converted to octal by repeated division of the number by 16 while
recording the remainder.

Example:

Reading the remainders from bottom to top we get,


42310 = 1A716

Binary to Octal and Vice Versa


To convert a binary number to octal number, these steps are followed −
● Starting from the least significant bit, make groups of three bits.
● If there are one or two bits less in making the groups, 0s can be added after the most
significant bit
54
● Convert each group into its equivalent octal number

Example:

101100101012 = 26258

To convert an octal number to binary, each octal digit is converted to its 3-bit binary equivalent
according to this table.

Example:
Octal Digit 0 1 2 3 4 5 6 7

Binary Equivalent 00 00 01 01 10 10 11 11
0 1 0 1 0 1 0 1
546738 = 1011001101110112

Binary to Hexadecimal
To convert a binary number to hexadecimal number, these steps are followed −
● Starting from the least significant bit, make groups of four bits.
● If there is one or two bits less in making the groups, 0s can be added after the most
significant bit.
● Convert each group into its equivalent octal number.

Example:

101101101012 = DB516
To convert an octal number to binary, each octal digit is converted to its 3-bit binary equivalent.

55
IEEE-based Integer and Floating point representations

Integers
An integer is a whole number (not a fraction) that can be positive, negative, or zero. Therefore,
the numbers 10, 0, -25, and 5,148 are all integers.

Integers are a commonly used data type in computer programming. For example, whenever a
number is being incremented, such as within a "for loop" or "while loop," an integer is used.
Integers are also used to determine an item's location within an array.

When two integers are added, subtracted, or multiplied, the result is also an integer. However,
when one integer is divided into another, the result may be an integer or a fraction. For example,
6 divided by 3 equals 2, which is an integer, but 6 divided by 4 equals 1.5, which contains a
fraction. Decimal numbers may either be rounded or truncated to produce an integer result.

Floating Point numbers


As the name implies, floating point numbers are numbers that contain floating decimal points.
For example, the numbers 5.5, 0.001, and -2,345.6789 are floating point numbers.

Computers recognize real numbers that contain fractions as floating point numbers. When a
calculation includes a floating point number, it is called a "floating point calculation." Older
computers used to have a separate floating point unit (FPU) that handled these calculations, but
now the FPU is typically built into the computer's CPU.

Floating point arithmetic


Arithmetic operations on floating point numbers consist of addition, subtraction, multiplication
and division the operations are done with algorithms similar to those used on sign magnitude
integers (because of the similarity of representation) -- example, only add numbers of the same
sign. If the numbers are of the opposite sign, you must do subtraction.

Addition
Example on decimal value given in scientific notation:
3.25 x 10 ** 3
+ 2.63 x 10 ** -1
-----------------
First step: Align decimal points

Second step: add

3.25 x 10 ** 3
+ 0.000263 x 10 ** 3

56
--------------------
3.250263 x 10 ** 3
(Presumes use of infinite precision, without regard for accuracy)

Third step: normalize the result

Example on fl pt. value given in binary:


.25 = 0 01111101 00000000000000000000000
100 = 0 10000101 10010000000000000000000

To add these fl. pt. representations,


Step 1: Align radix points
Shifting the mantissa LEFT by 1 bit DECREASES THE EXPONENT by 1
Shifting the mantissa RIGHT by 1 bit INCREASES THE EXPONENT by 1

We want to shift the mantissa right, because the bits that fall off the end should come from the
least significant end of the mantissa
-> Choose to shift the .25, since we want to increase its exponent.
-> Shift by 10000101
-01111101
---------
00001000 (8) places.

0 01111101 00000000000000000000000 (original value)


0 01111110 10000000000000000000000 (shifted 1 place)
(note that hidden bit is shifted into msb of mantissa)
0 01111111 01000000000000000000000 (shifted 2 places)
0 10000000 00100000000000000000000 (shifted 3 places)
0 10000001 00010000000000000000000 (shifted 4 places)
0 10000010 00001000000000000000000 (shifted 5 places)
0 10000011 00000100000000000000000 (shifted 6 places)
0 10000100 00000010000000000000000 (shifted 7 places)
0 10000101 00000001000000000000000 (shifted 8 places)

Step 2: Add (don't forget the hidden bit for the 100)
0 10000101 1.10010000000000000000000 (100)
+ 0 10000101 0.00000001000000000000000 (.25)
---------------------------------------
0 10000101 1.10010001000000000000000
Step 3: normalize the result (get the "hidden bit" to be a 1)
It already is for this example.

The result is

57
0 10000101 10010001000000000000000

Subtraction
Like addition as far as alignment of radix points then the algorithm for subtraction of sign mag.
numbers takes over.

Before subtracting, compare magnitudes (don't forget the hidden bit!)


Change sign bit if order of operands is changed. Don't forget to normalize numbers afterward.

Multiplication
Example on decimal values given in scientific notation:
3.0 x 10 ** 1
+ 0.5 x 10 ** 2
-----------------

Algorithm: multiply mantissa, add exponents


3.0 x 10 ** 1
+ 0.5 x 10 ** 2
-----------------
1.50 x 10 ** 3

Example in binary: Use a mantissa that is only 4 bits so that you don’t take long doing the
multiplication part.
0 10000100 0100
x 1 00111100 1100
-----------------
Mantissa multiplication: 1.0100
(don't forget hidden bit) x 1.1100
------
00000
00000
10100
10100
10100
---------
1000110000
becomes 10.00110000

Add exponents: Always add true exponents (otherwise the bias gets added in twice)

Biased:
10000100

58
+ 00111100
----------
10000100 01111111 (switch the order of the subtraction,
- 01111111 - 00111100 so that we can get a negative value)
---------- ----------
00000101 01000011
true exp true exp
is 5. is -67
Add true exponents 5 + (-67) is -62.
Re-bias exponent: -62 + 127 is 65.

Unsigned representation for 65 is 01000001.

Put the result back together (and add sign bit).


1 01000001 10.00110000

Normalize the result:


(Moving the radix point one place to the left increases the exponent by 1.)

1 01000001 10.00110000
becomes
1 01000010 1.000110000

This is the value stored (not the hidden bit!):


1 01000010 000110000

Division
 Similar to multiplication.
 True division: do unsigned division on the mantissas (don't forget the hidden bit)
 Subtract TRUE exponents

The IEEE standard is very specific about how all this is done. Unfortunately, the hardware to
do all this is very slow.

Some comparisons of approximate times:


2's complement integer add 1 time unit
fl. pt add 4 time units
fl. pt multiply 6 time units
fl. pt. divide 13 time units

There is a faster way to do division. It’s called division by reciprocal approximation. It takes
about the same time as a fl. pt. multiply. Unfortunately, the results are not always the same as
with true division.
59
Division by reciprocal approximation:
Instead of doing a / b, they do a x 1/b.
Figure out a reciprocal for b, and then use the fl. pt.

Example of a result that isn't the same as with true division.


True division: 3/3 = 1 (exactly)
Reciprocal approx: 1/3 = .33333333

3 x .33333333 = .99999999, not 1

It is not always possible to get a perfectly accurate reciprocal.

Rounding
Arithmetic operations on fl. pt. values compute results that cannot be represented in the given
amount of precision. So, we must round results.

There are MANY ways of rounding. They each have "correct" uses, and exist for different
reasons. The goal in a computation is to have the computer round such that the end result is as
"correct" as possible.

Methods of rounding:
Round toward 0 -- also called truncation.
Figure out how many bits (digits) are available. Take that many bits (digits) for the result and
throw away the rest. This has the effect of making the value represented closer to 0.

Example:
.7783 If 3 decimal places available, .778
If 2 decimal places available, .77

Round toward + infinity -- regardless of the value, round towards +infinity.

Example:
1.23 If 2 decimal places, 1.3
-2.86 if 2 decimal places, -2.8

Round toward - infinity -- regardless of the value, round towards -infinity.

Example:
1.23 If 2 decimal places, 1.2
-2.86 if 2 decimal places, -2.9

In binary -- rounding to 2 digits after radix point

60
Round toward + infinity --
1.1101
|
1.11 | 10.00
------
1.001
|
1.00 | 1.01
-----

Round toward - infinity --

1.1101
|
1.11 | 10.00
------
1.001
|
1.00 | 1.01
-----
Round toward zero (TRUNCATE) --
1.1101
|
1.11 | 10.00
------
1.001
|
1.00 | 1.01
-----
-1.1101
|
-10.00 | -1.11
------
-1.001
|
-1.01 | -1.00
-----
Round toward nearest --

ODD CASE:
If there is anything other than 1000 to the right of the number of digits to be kept, it is
rounded in IEEE standard such that the least significant bit (to be kept) is a zero.

61
1.1111
|
1.11 | 10.00
------
1.1101
|
1.11 | 10.00
------
1.001 (ODD CASE)
|
1.00 | 1.01
-----
-1.1101 (1/4 of the way between)
|
-10.00 | -1.11
------
-1.001 (ODD CASE)
|
-1.01 | -1.00
-----
Integer Representation and Arithmetic

Integer Representation
● Bit-- Binary Digit
○ 1 byte = 8 bits
○ 1 word = 2 bytes
● Integer takes up two bytes; can be signed or unsigned.

Unsigned Integers
● Can represent whole numbers from 0 to 65,535
○ (0 to 216 - 1).
● In binary, this is from
○ 02to 11111111111111112
● Internally, binary representation of decimal value is 16 bits.

Signed Integers
● Need to reserve one bit for the sign.
● Three ways:
○ Sign-Magnitude
○ 1's Complement
○ 2's Complement
Sign-Magnitude
● Uses the most significant bit of the word to represent the sign.

62
○ 0 - Positive
○ 1 - Negative.
● Rest of the number is encoded in magnitude part
37 = 00000000 00100101
-37 = 10000000 00100101
6712 = 00011010 00111000
-6712 = 10011010 00111000
● Can represent numbers from -32,767 to 32,767.
○ -215+1 .. 215-1
● But, two representations for zero:
0 = 00000000 00000000
● -0 = 10000000 00000000 Arithmetic can be cumbersome.

1's Complement
● Negative numbers are stored as bitwise complement of corresponding positive numbers.
● Leftmost bit of the positive number is 0. That of the negative number is 1.
196 = 00000000 11000100
-196 = 11111111 00111011
● Can represent numbers from -32,767 to 32,767.
○ -215+1 .. 215-1
● Arithmetic is easier than sign-magnitude.
● But, still have two representations for zero:
0 = 00000000 00000000
-0 = 11111111 11111111

2's Complement
● Modern Method: Positive number represented in same way as other two methods
● Negative number obtained by taking 1's Complement of positive number and adding 1.
6713 = 00011000 00011101
1's Comp = 11100111 11100010
2's Comp = 11100111 11100011
● Word integers can represent numbers from -32,768 to 32,767.
○ -215 .. 215-1
● Byte integers can represent numbers from -128 to 127.
○ -27 .. 27-1
● One version of zero:
00000000 00000000

Conversion of Byte Integer to Word


● Sign Extension
● Copy sign bit of the byte into all the bits of the upper byte of the word.
37 = 00100101 -> 00000000 00100101
● -37 = 11011011 -> 11111111 11011011 cbw

63
○ converts the signed byte in AL to a word in AX

Conversion of Word Integer to Byte


● Remove the upper byte of the word. Retain only the lower byte.
● Meaningful only if the original number can be represented by a byte.

Integer Arithmetic (1's Comp and 2's Comp)


● Addition: Simply add the two binary representations.
● Subtraction: Find negative of one number, add to the second.

Addition in 1's Comp


● Add binary representations of the two numbers.
● If there is a carry, add it back in on the right side.
51 00110011
+ (-37) + 11011010
----------
1 00001101
+1
----------
14 00001110

Addition in 2's Comp


● Add binary representations of the two numbers.
● Disregard the carry.
51 00110011
+ (-37) + 11011011
----------
14 1 00001110

Subtraction in 2's Comp


Overflow
● If two numbers have different signs, their sum will never overflow.
● If they have the same sign, they might overflow.
● Overflow has occurred if sign of result is different than sign of addends.

Logic operators and Logic Operations


A logical operator is a special symbol or word that connects two or more phrases of information.

We have a range of the logic operators which include OR, AND, NOT, NOR, XOR and XNOR.

64
OR
The OR logic operation returns True if either its inputs are True. If all inputs are false, the output
is also false. In computer programming, the OR operation is usually written as || (two vertical
bars). In Boolean algebra, the OR value of two inputs A and B can be written as A+B.

Note Do not mistake the OR operation for arithmetic addition, even though they both use the "+"
symbol. They are distinct operations.

Below is the circuit diagram of an OR logic gate.

OR logic gate

AND
The AND logic operation returns True only if either of its inputs are True. If either of the inputs
is False the output is also false. In computer programming, the AND operation is usually written
as && (two ampersands). In Boolean algebra, the AND operation of two inputs A and B can be
written as AB.

Below is the circuit diagram of an AND logic gate.

OR logic gate

NOT
The NOT logic operation returns True if its input is False, and False if its input is True. In
computer programming, the NOT operation is usually written as ! (an exclamation mark). In
Boolean algebra, the NOT value of an input A can be written as A̅ (A with an overscore).

Below is the circuit diagram of a NOT logic gate.

NOT logic gate

NOR
65
The NOR logic operation “stands for NOT OR” returns true if either of its inputs are false, and
false if either of its inputs are true. In Boolean algebra, the NOR value of two inputs A and B can
be written as (A+B with an overscore). NOR has the distinction of being one of two
"universal" logic gates, because any other logic operation can be created using only NOR gates.
(The other universal logic gate is NAND.)

Below is the circuit diagram of a NOR logic gate.

NOR logic gate

XOR
The XOR logic operation (which stands for "Exclusive OR" returns true if either of its inputs
differ, and false if they are all the same. In other words, if its inputs are a combination of true and
false, the output of XOR is true. If its inputs are all true or all false, the output of XOR is false. In
Boolean algebra, the XOR value of two inputs A and B can be written as A⊕B (the XOR symbol,
resembles a plus sign inside a circle).

Below is the XOR operation circuit diagram.

Exclusive OR gate

XNOR
The XNOR logic operation (which stands for "Exclusive NOT OR" returns true if either of its
inputs are the same, and false if either of them differ. In other words, if its inputs are a
combination of true and false, the output of XNOR is false. If its inputs are all true or all false,
the output of XNOR is true. In Boolean algebra, the XNOR value of two inputs A and B can be
written as (the XOR symbol ⊕, resembles a plus sign inside a circle with a line over
everything).

Below is the XNOR operation circuit diagram.

Exclusive NOT OR gate

66
Karnaugh map
A Karnaugh map provides a pictorial method of grouping together expressions with common
factors and therefore eliminating unwanted variables. The Karnaugh map can also be described as
a special arrangement of a truth table.

The diagram below illustrates the correspondence between the Karnaugh map and the truth table
for the general case of a two variable problem.

The values inside the squares are copied from the output column of the truth table, therefore there
is one square in the map for every row in the truth table. Around the edge of the Karnaugh map
are the values of the two input variable. A is along the top and B is down the left hand side. The
diagram below explains this:

The values around the edge of the map can be thought of as coordinates. So as an example, the
square on the top right hand corner of the map in the above diagram has coordinates A=1 and
B=0. This square corresponds to the row in the truth table where A=1 and B=0 and F=1. Note
that the value in the F column represents a particular function to which the Karnaugh map
corresponds.

Conclusion
At the end of this learning outcome, the learner should be able to explain number systems,
demonstrate integer and floating point representations according to IEEE standard explain

67
integer and floating point arithmetic, logic operators and logic operations and demonstrate
methods of representing logic operations.

Further Reading
1. [Link]
2. [Link]

[Link] Self-Assessment

Written Assessment
1. Which of the following is not a logical operator?
a. And
b. OR
c. Else
d. NOT

2. Boolean operator which combines search terms so that each search result contains all of
terms, is
a. and operator
b. not operator
c. or operator
d. nor operator
3. The sign magnitude representation of -1 is?
a. 1001
b. 1000
c. 0001
d. 1110
4. The ALU gives the output of the operations and the output is stored in the?
a. Memory devices
b. Registers
c. Flags
d. Output Unit
5. The bitwise complement of 0 is?
a. 00000001
b. 11111111
c. 11111110
d. 10000000

Oral Assessment
1. Describe the logical operators
2. Describe with illustrations how to convert Decimal to binary
68
Practical Assessment
Research on the application areas of logic operations in computer science.

[Link] Tools, Equipment, Supplies and Materials


● Computer
● Internet connectivity
● Calculator
● Stationery
● Instruction material

[Link] References

1. [Link]
2. [Link]
3. [Link]

69

You might also like