Chapter 5

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

Chapter 5: Computer Arithmetic

Chapter 5
Computer Arithmetic
- computers are built from components that have two values; on/off
- the binary number system, which has only two digits 0 and 1 is used to express
the two states
- internal arithmetic operations of a digital computer are actually performed in
binary number system

I. Word and Byte


- bits are the smallest quantity of information that can be stored and manipulated by
computer
- BIT – Binary digit
- Word – a group of bits that can be stored or retrieved from storage location or
cells
o Is a basic unit of information
o Usually 8 bit (1 byte), 16 bit (2 byte), 32 bit
o Can represent character data or numeric data
 Characters may be classified as:
 Letters and/or digits (A-Z, a-z, or 0-9)
 Special symbols such as: ?, *, +
 Control characters (which control devices)
- When characters are used in computer, they must be converted into a binary string
(sequences of 0’s and 1’s)
- There are several codes for converting data into binary string:
E.g
o ASCII (American Standard Code for Information Interchange)
o EBCDIC (Extended Binary Code Decimal Interchange Code)
o BCD ( Binary Coded Decimal
 In ASCII code, a character is represented by 7 bits - maximum of 27 different
characters out of which 96 are printable and 32 non printable
o E.g ASCII code of A=65=1000001
 EBCDIC uses broader numerical range (0-255)
o Uses 8 bits to represent a character
o E.g EBCDIC code of A= 193=11000001

II. Radix Number System (Base Numeration)


- The decimal number system with which everyone is familiar uses the number 10
as its base or radix and has 10 symbols or digits 0-9.
- e.g. a decimal number X is written as an an-1… a1 a0.a-1a-2… a-m, where a’s are one of
10 possible digits
- its general form and usual interpretation is:
- place value
10n… 101 100. 10-110-2... 10-m
An… a1 a0 . a-1 a-2 … a-m
- the general form is: X= an * 10n + … + a1*101+a0*100 + a-1 * 10-1 + … + a-m * 10-m
n

Page 1 of 8 By:- Ahmed A.


Chapter 5: Computer Arithmetic

= ∑ ai * 10i
i= -m

e.g.
- the decimal number 852.37 is equal to 8*102 +5 *101+ 2*100 +3*10-1 + 7*10-2
- Likewise, substituting any base in place of 10 will give the general form of a
number to that radix.
- In computer science 4 bases are of concerns: base 2, base 8, base 10 and base 16.
And the number systems in these bases are binary, octal, decimal and
hexadecimal respectively.
- Summary
Base Number System Possible Symbols(digits)
2 Binary 0 and 1
8 Octal 0-7
10 Decimal 0-9
16 Hexadecimal 0-9 and A-F
- Base n number has n possible digits and the digits are 0 to n-1
III. Conversion from One Base to Another
Step 1. Convert a given number to decimal number
Step 2. Convert the obtained no to the desired base

A. Conversion of a Number to a Decimal Number


Step 1. Represent the given number in its general form
Step 2. Using decimal arithmetic simplify the general form
e.g convert the following numbers to decimal
a) (101.1)2 d) (12B) 16
b) (120.2)3 e) (127)8
c) (234) five f) (192)10
 A number expressible with finite number of digits in one base may
not necessarily transform into a finite number of digits in another
bases

B. Conversion from Base 10 to Another Bases


- A decimal number might have two parts: integrated and fractional parts.
Steps to convert from base 10 to any base
Step 1. Converting the integral part of the decimal into integral part in the desired
base
Step 2. Convert the fractional part of the decimal to a fractional number part in the
desired base
Step 3. The number in the desired base consists of the integer obtained in step 1
followed by a base point and then followed by the fractional number part obtained in
step 2
Step 1 Explained:
To convert an integer to a base B integer number
1. Divide the given integer by the base B

Page 2 of 8 By:- Ahmed A.


Chapter 5: Computer Arithmetic

2. Repeat step 1 until a 0 quotient is obtained, using at each stage the quotient from
the previous stages as dividend
The successive remainders (starting from the least significant end) are the successive
digits of the number in base B

e.g convert
a) 67 to binary
b) 256 to binary
c) 256 to hexadecimal
Step 2 Explained:
To convert a decimal fraction to a fraction in base B
1. Multiply the given decimal fraction in base B
2. Repeat step 1 using the multiplicand at each step the remaining fractional part
from the previous step until a fractional part is identically 0, or until as many
digits as desired have been generated. The successive integral parts are the
successive digits of the number in base B starting from the most significant
end.
E.g convert
a) (.65)10 to binary
b) (.44)10 to quinary
c) (.32)10 to hexadecimal
Solution
.65 *2 = 1.3
.30 * 2 = .60
.60 * 2 = 1.20
.20 * 2 = .40
.40 * 2 = .80
.80 * 2 = 1.60
.60 * 2 = 1.20
.
.
. ____
Ans: (101001)
E.g Convert a) (256.65)10 to binary
b) (256.32)10 to hexadecimal
c) (10011.101)2 to quinary

C. Some Simple Algorithms


Binary- to-Octal
- group the binary digits in triplets starting at the binary point and convert each of
these to its octal equivalent
o you can add 0’s on the right and left, if necessary, to form a complete
triplet
o e.g. a) (110 011.010 110)2 = (63.26)8
Octal-to-Binary
- writing the binary triplets for each octal digits

Page 3 of 8 By:- Ahmed A.


Chapter 5: Computer Arithmetic

- e.g. a) (63.26)8 = (110 011.010 110)2


b) (65.61)8 = (110 101.110 001)2
Binary-to-Hexadecimal
- group the binary digits in quadriplets starting at the binary point and convert each
of these to its hexadecimal equivalent
o you can add 0’s on the right or left, if necessary, to form a complete
quadriplets
e.g. (11 0011.0101 10)2 = (33.58)16
(11 1100 1011.1001 1)2 = (3CB.98)16
Hexadecimal-to-Binary
- write the binary quadruplet for each hexadecimal digits
e.g.
a) (33.58)16 = (0011 0011.0101 1000)2
b) (4E93)16 = (0100 1110 1001 0011)2
Octal-to-Hexadecimal
- octal-to-binary-to-hexadecimal
- convert (65)16 to octal
Hexadecimal-to-Octal
- hexadecimal-to-binary-to-octal
- e.g. convert (33)16 to octal
Remark: when you convert signed number, ignore the sign during conversion. After
the number in the desired base has been obtained, affix the sign of the original
number to the desired number
E.g. convert (-125)10 to base 5
Notice: before you start converting a number to a specified base, check whether the
given number is legal one
e.g. (230)2 is illegal and same is true for (98)8
BCD codes
- represent a numerical value
- is a 4 bit code
- in BCD code each decimal digit is coded into groups of 4 bits
- to convert a decimal number to BCD forms, simply encode each decimal digit
into groups of 4 bits
e.g. 129 = 0001 0010 1001

Exercise
1. convert 192 to base 2
2. convert 192 to base 8
3. convert 192 to base 16
4. convert (100010101110.11011101)2 to decimal
5. convert (100010101110.11011101)2 to octal
6. convert (100010101110.11011101)2 to hexadecimal
7. convert (ABEBE)16 to decimal
8. convert (ABEBE)16 to binary
9. convert (ABEBE)16 to octal
10. convert( 256)2 to octal

Page 4 of 8 By:- Ahmed A.


Chapter 5: Computer Arithmetic

Representing Negative Numbers


4 systems are there
1. sign and magnitude
2. ones complement
3. twos complement
4. excess 2m-1
1. Sign and Magnitude representation
- The first bit is reserved for sign such that 0 for positive and 1 for negative
E.g. 24 = 00011000 and -24 = 10011000
- For an N-bit word, there are 2N possible different values ranging from 0 to 2N-1
- If one bit is reserved for sign, the remaining N-1 bits represent the number. Thus,
the range of sign and magnitude number in N bits is given by –(2N-1-1) to 2N-1-1
=> -2N-1+1 to 2N-1-1
- E.g. a 3 bit number can represent from -3 to 3 ,i.e., from 111 to 011
- N.B 000 = +0 and 100 = -0
- During arithmetic computation in sign and magnitude numbers, the sign bit is not
involved
- E.g.
12 001100 01100
+6 + 000110 + 00110
18 10010 010010
2. Ones Complement Representation
- The ones complement of a binary number is obtained by changing each zero to
one and vise versa of the number including the sign bit
E.g.
-4 in one complement in 4 bits:
Binary representation of 4 is 0100
-4 in ones complement is 1011
- N.B All 0 or all 1 means 0 in ones complement
3. Twos Complement Representation
- the twos complement of a binary number is obtained by adding 1 to its ones
complement
- e.g -4 in twos complement in 4 bits
- 0100(binary) 1011(ones complement) 1011+1=1100(twos complement)
4. Excess 2 Representation
m-1

- m is number of bits
- the excess of 2m-1 represents a number by storing it as the sum of itself and 2m-1
- e.g. for 5 bit number, the representation is called excess 16(25-1)
- -4 = -4 + 16 = 12 and -4 is represented by the 5 bit binary number for 12,i.e.
01100
- Exercise
- Represent -9 in:
a) Signed magnitude. Ans. 11001
b) Ones complement. Ans. 10110
c) Twos complement. Ans. 10111
d) Excess 16. Ans.00111

Page 5 of 8 By:- Ahmed A.


Chapter 5: Computer Arithmetic

Subtraction by Use of Complements


- complements are mainly used for representing negative numbers and subtraction
- subtraction is achieved by adding the complement
- to subtract any number (positive or negative), substitute the required complement
for the numbers to be subtracted and then add
- the result is
a) (n=1) bit number and the arithmetic is in
1. Ones complement the (n+1)th bit ( a carry) is added to the right most
bit of the result. This process is called an end-round carry.
2. twos complement discard the (n+1)th bit
b) n bit number and the arithmetic is in
1. Ones complement, to read the binary value calculate the ones
complement of the magnitude bit and place a minus sign in front of it.
2. Twos complement, to read the binary value calculate the twos
complement of the magnitude bits and place a minus sign in front of it.
e.g. let A= 12, B= 6 perform the following in ones and twos complements
in 5 bits
a) A-B b) B-A c)-A-B
Solution
Binary representation of A= 01100, B= 00110
Ones complement of –A=10011, -B=11001
Twos complement of –A= 10100, -B= 11010
Decimal Ones complement Twos complement
a) 12 01100 01100
-6 +11001 +11001
6 1 00101 1 00101
+1 Discard
00110

b) 6 00110 00110
-12 +10011 +10100
-6 11001 11010

c) -12 10011 10100


-6 +11001 +11010
-18 1 01100 0111 0
+1 1 Discard
01101 0111 0

N.B if the sign bits of A and B are the same but the sign of the result is different,
arithmetic overflow occurs. In case of “c” arithmetic overflow occurred.

Page 6 of 8 By:- Ahmed A.


Chapter 5: Computer Arithmetic

Floating Point Numbers


- The two basic methods of binary number representation in computers are integer
representation and floating point representation.
- Floating point numbers are numbers in the normalized form: A * bx, where;
A is mantissa
B is base (radix)
X is exponent
E.g. The decimal number 46.5 = 0.465*102
Mantissa=0.465
Base=10
Exponent=2
The binary number 1101.11 = 0.0110111*24
Mantissa = 0.0110111
Base =2
Exponent=4
- floating point representation
Sign Exponent Mantissa
- exponent is represented by a biased exponent which is expressed by:
True exponent + excess 2n-1, n is the number of bits representing the
exponent
- true exponent + excess 2n-1 is called characteristics
E.g1.convert 9.5 to normalized binary floating point form using 7 bit
exponent and 16 bit mantissa
Solution
9= (1001)2 = 0.1001 * 24
0.5 = (0.1)2 = 0.1 *20
Hence, normalized exponential form of 9.5 is 0.10011 * 24
Characteristic = true exponent + excess 2n-1
= 4 + 27-1
=68= (1000100)2
9.5 is represented as:
0 1000100 10011000…0
----7 bits-- ---16 bits------
E.g2. convert -18.375 to normalized binary floating point form using 7 bit
exponent and 16 bit mantissa.
Solution
18 = (10010)2 = 0.10010 * 25
0.375 = (0.011)2 = 0.011 * 20

Page 7 of 8 By:- Ahmed A.


Chapter 5: Computer Arithmetic

The normalized form of -18 = -0.10010011 * 25

Characteristic = true exponent + excess 27-1

=5 + 64 = 69 = (1000101)2
Thus, the representation of -18.375 is:

1 1000101 10010011000…0
---7 bits------- ----------16 bits----------

- If the mantissa has fewer digits than the required bit positions, zeros are added to
the end of the mantissa
- If the characteristic has fewer digits than the required bit positions, zeros are
added to the beginning of the characteristic

- Exercise
1. Perform the following operations in binary
a. 1101.01 + 11.11
b. 1101.01 * 11.11
2. Perform the following subtraction using twos complement in 6 bits
a. 111100 – 111011
b. 1101.010 – 11.1101
3. Repeat problem 2 using 1’s complement
4. convert 4.375 to normalized binary floating point form (7 bit exponent,
16 bit mantissa)
5. convert -4.350 to normalized binary floating point form (8 bit
exponent and 27 bit mantissa)

Page 8 of 8 By:- Ahmed A.

You might also like