CHAPT4 1 Data Structures in Computers
CHAPT4 1 Data Structures in Computers
COMPTER ENGINEERING
Data Representations
Most computers support a variety of data types.
Numeric: Integer: - Unsigned, signed
Binary Coded Decimal (BCD)
binary, ternary, octal,
Fractions
Floating point
Base Conversion
Radix R - to - Decimal: Compute the expansion
Given a number with n total number of digits of radix R, and d be any
Decimal - to - Radix R
Successively divide the decimal number by the target radix and note the
remainder until the quotient is 0
The method is called base conversion with the remainder method
Last remainder is considered to be the most significant bit (msb) and the
first remainder to least significant bit (lsb)
Base Conversion
Decimal - to - Radix R (continuing)
e. g. Convert 8210 into radix 6.
Here target radix is 6
Integer
82 6
=
13
13 6
=
2
26
=
0
8210 = 2146
Remainder
4 --------- lsb
1
2 --------- msb
Radix R1 to Radix R2
Convert R1 to decimal first, then obtained decimal to R2:
that is [R1 to decimal to R2]
Base Conversion
Binary fraction to decimal
e. g. Convert 0.10112 to decimal
0.10112
=
[1 21] + [0 22] + [1 23] + [1 24]
=
0.5 + 0 + 0.125 + 0.0625
=
0.687510
Base Conversion
Non-terminating base 2 fraction
Sometimes 1.00 will not occur. In this case the process is
terminated after a suitable number of decimal places.
e. g. Convert 0.210 to binary; [here target radix is 2]
0.2 2
0.4 2
0.8 2
0.6 2
0.2 2
0.4 2
0.210
=
=
=
=
=
=
=
Product
Carrier
0.4
0 --------- msb
0.8
0
1.6
1
1.2
1
0.4
0 --------- lsb
0.8
repeating, no termination
0.00112
Base Conversion
Binary to Hexadecimal
Hexadecimal numbers are based on the hexadecimal
counting system formed on a base of 16
Consists of 16 different symbols : 0 9,A,B,C,D,E and F;
each corresponding to decimal numbers 0 15
They are used to represent or remember multi-bit binary
numbers
How:
Write down the full binary numbers
Split the number into groups of 4 bit each staring from
lsb
Convert each group into its equivalent decimal number,
and substitute the equivalent Hexadecimal digit for
each group
Base Conversion
Binary to Hexadecimal (continuing)
e. g. Convert 11100011011011012 to base 16
1110 0011 0110 1101
14
3
6
13
E
3
6
D
1110001101101101
=
E36D16
Exercise:
Convert 23.37510 to base 2 and prove if it is correct by
converting back the answer in base 2 to decimal
Binary
(base 2)
0
1
10
11
100
101
110
111
1000
1001
1010
1011
1100
1101
1110
1111
Octal
(base 8)
0
1
2
3
4
5
6
7
10
11
12
13
14
15
16
17
Hexadecimal
(Base 16)
0
1
2
3
4
5
6
7
8
9
A
B
C
D
E
F
= [102][112]
= 234
1010102 =
[1012][0102]
1010102 =
[102][102][102] = 2224
234 = [24][34]
= 528
= [102][112] = 10112
Sign-Magnitude
N bits can represent values from -(2 N-1 -1) to +(2N1 -1). There are two representations for 0; +0 and
-0.
one bit, usually the leftmost bit, represents the
sign of the number (0 for +, 1 for -), the remaining
bits specify the magnitude of the number. e. g.
+ 25 = 000110012 , msb = 0 showing a +ve number
- 25 = 100110012, msb = 1 showing a -ve
Sign-Magnitude (cont.)
Two representations for zeros:
+ 0 = 000000002
- 0 = 100000002
Ones Compliment
N-bits can represent values from; -(2 N-1-1) to +(2N-1
-1)
The negative of a value is obtained by inverting or
complementing each bit [0 to 1 and 1 to 0]
The procedure goes both ways from +ve to ve or
from ve to +ve
The msb is the sign bit [0 for +ve, 1 for ve]
Two representation for zero:
+0 = 000000002
-0 = 111111112
Twos Complement
N-bits number can represent values from: -2n-1 to
+(2n-1 -1)
The negative of a value is obtained by adding 1
to the ones compliment negative. Perform Ones
complement first the add 1
The procedure goes both ways from +ve to ve or
from ve to +ve
The msb is the sign bit [0 for +ve, 1 for ve]
One representation for zero:
+0 = 000000002
-0 = 000000002
00000001 2
Answer
= 11100111 2
Benefits
One representation of zero
Arithmetic works easily (see later)
Negating is fairly easy
3 = 00000011
Boolean complement gives
Add 1 to LSB
11111100
11111101
0=
00000000
Bitwise not
11111111
Add 1 to LSB
+1
Result
1 00000000
Overflow is ignored, so:
-0=0
-128 =
10000000
bitwise not
01111111
Add 1 to LSB
+1
Result
10000000
So:
-(-128) = -128 X
Monitor MSB (sign bit)
It should change during negation
= 1 0111 0100
overflow ignored
Normalization
Normalized representation places the
radix point immediately to the left of the
left most nonzero digit in the fraction
e. g. 254 = 0.254 103
Exponent (e)
Significand or Mantissa
Coding
Coding is the representation of characters via a
character code in which each character is assigned
a unique combination of bits
Three most popular codes are:
BCDIC = Binary Coded Decimal Interchange Code
EBCDIC = Extended Binary Coded Decimal
Interchange Code
ASCII
= American Standard Code for Information
Interchange
Unicode
ASCII:
Coding
EBCDIC:
Is an 8 bit code (256 characters)
Used on mainframe IBM machine
Unicode:
is a 16 bit standard
can represent 65 thousand characters of
which 49,000 have been defined
incorporates ASCIII as subnet
Example ASCII coding:
ASCII Coding
CAD = 43414216
= 0100 0011 0100 0001 0100 00102
f1
= 463116
= 0100 0110 0011 00012
3415 = 3334313416
= 0011 0011 0011 0100 0011 0001
01012
Note that this is a text strong and no
arithmetic may be done in it
symbo
l
Code
41
4
2
43
44
55
46
30
31
32
33
34
35
36
37
COMPUTER ARTHMETIC
Four basic arithmetic operations in a computer
Addition, subtraction, multiplication, and division
Example of an Overflow
Add 8010 + 8010
Using decimal addition: 80 + 80 = 16010
Using binary: 80 = 0101 0000
80 = 0101 0000
160 = 1010 00002 = - 9610
How: Msb = 1 hence a negative number
Taking the twos compliment of 1010 000
= 0110 0000 = - 9610
0
1
0
1
Carry
Sum
0
0
0
1
0
1
1
0
0
0
0
1
0
0
0
1
1
0
0
1
1
0
Carry
Sum
0
0
0
1
0
1
1
1
0
1
1
0
1
0
0
1
Ripple-Borrow Subtractor
A ripple-borrow subtractor can be
composed of a cascade of full
subtractors.
Two binary numbers A and B are
subtracted from right to left, creating
a difference and a borrow at the
Rules Full
outputs of each full Subtraction
subtractor
for
subtractor
each bit position.
a +b +
diff
bor
i
I+1
bori
0
0
0
0
1
1
0
0
1
1
0
0
0
1
0
1
0
1
0
0
0
1
0
1
0
1
1
0
1
0
Ripple-Borrow Subtractor
Combined Adder/Subtractor
A single ripple- carry adder can perform
both addition and subtraction, by forming
the twos complement negative for B when
subtracting. (Note that +1 is added at c0 for
twos complement.)
(- 12)
0 1 1 0 12
(+13)
100000
+
1 ------------ End-around carry
0001
(+1)
Multiplication
Complex
Work out partial product for each digit
Take care with place value (column)
Add partial products
Multiplication of two 4-bits unsigned
binary integers produces an 8-bits result
Multiplication of two 4-bit signed binary
integers produces only a 7-bit result (each
operand reduces to a sign bit and a 3-bit
magnitude for each operand, producing a
sign-bit and a 6-bit result).
Multiplication Example
1011
x 1101
1011
0000
1011
1011
10001111
Note: need
Multiplicand (1110 )
Multiplier
(1310)
Partial product
Partial product
Partial product
Partial product
Product (143 10)
double length result
Shift
C
Register
P
0000
1101
0110
0011
1001
1001
0100
0001
1000
Register
A or Q
1 0 1 1-------- Initial values. Note lsb of A is 1, then
1 0 1 1------- 1(a): add B to P, and sum to P
1 1 0 1 --------1(b): Shift one step right. Note Lsb of A is 1,then
1 1 0 1 --------2(a): Add B to P, and sum to P
1 1 1 0---------2(b): Shift one step right, Note lsb is 0, then
1 1 1 0---------3(a): Add 0000 to P, and sum to P, or skip addition
1 1 1 1---------3(b): Shift one step right, lsb of A is 1, then
1 1 1 1---------4(a): Add B to P, and sum to P
1 1 1 1---------4(b): Shift one step right
C, P = 0
B Multiplicand
A Multiplier
Count n
No
Yes
A0 =
1?
P= B + P
Shift right:
C, P, A.
Count = Count - 1
No
Yes
Count =
0?
End
Product= P, A
Serial Divider
Operates the division of an unsigned number and
produces the quotient bits one at a time
From a b (a/b), put a in register A, b in
register B, 0 in register P and then perform n
divide steps
Each divide step consists of four parts:
Shift the register pair (P, A) one bit left
Subtract the content of register B (which has number b)
from register P, putting the result back into P
If the result of step 2 is negative, set the low-order bit
of A to 0, otherwise to 1
If the result of step 2 is negative, restore the old value
of P by adding the contents of register B back into P
Register
1
1
1
1
1
1
1
1
0
0
0
0
0
0
0
0
1
1
1
1
1
0
0
0
0
0
0
0
1
1
1
1
1
0
0
0
0
0
0
0
1
1
1
1
0
0
0
0
A
0 -----Initial values.
-----1(i): shift one step left
-----1(ii): subtract P B, result put in P.
0 -----1(iii): msb = 1, result is ve,set Quotient bit A 0 to 0
0 -----1(iv): step 2 is ve, restore the old value of P
------2(i): shift left
------2(ii): subtract P-B, result in P
1 -----2(iii): msb = 0, +ve result, set A 0 to 1
----- 3(i): shift left
------3(ii): subtract P-B result in P
0 -----3(iii): msb = 1, -ve result, A 0 (lsb) = 0
0 -----3(iv): Step 2 ve, restore the old value of P
-----4(i): Shift left
-----4(ii): subtract P-B, result in P
0 -----4(iii): msb = 1, -ve result, A0 (lsb) = 0
0 -----4(iv): restore the old value of P
00001101
Divisor
1011 10010011
1011
001110
Partial
1011
Remainders
001111
1011
100
Quotient
Dividend
Remainder
Solution 2
Booths algorithm
Booths Algorithm
Booths Algorithm forms the basis for a
number of signed multiplication algorithms
It is based upon recording the multiplier,y, to
a recoded, value ,z, leaving the the
multiplicand ,x, unchanged
In booth recoding, each digit of the multiplier
can assume negative as well as positive and
zero
There are especial notation called signed
digit (SD) encoding, to express these signed
digits
In SD encoding; +1 and 0 are expressed as a
1 and 0, but 1 is expressed as 1.
Booths Algorithm
e. g. booth recoding recodes the no 7 as 8-1
7 = 8-1 = 10002 00012 = 1001
Where 1 represents 1 as above
If M is the multiplicand, A the accumulator register, Q the
multiplier and Q-1 the extra appended bit on the right of Q,
then Booth's algorithm may be executed as shown below:
1. Initialization
a. Load A and Q-1 with zeros.
b. Load M with the multiplicand
c. Load Q with the multiplier
2. Repeat n times:
a. If the Q0 = 0 and Q-1 = 0, add 0 to A. else
b. if the Q0 =0 and Q-1 =1, Add M to A. else
c. If the Q0 =1 and Q-1 =0, Subtract M from A. else
d. If the Q0 = 1 and Q-1 = 1 add 0 to A
e. Shift AQQ-1 one bit right using sign extension.
Booths Algorithm
Division
More complex than multiplication
Negative numbers are really bad!
Based on long division
1.510 = 0.112 2
+1
Operands
Alignment
Normalization
6.144 102
0.06144 104 1.003644 105
+ 9.975 104 + 9.9975 104
10.03644 104
1.076 10-7 1.076 10-7
- 9.987 10-8 - 0.9987 10-7
0.0773 10-7
7.73 10-9
FP Arithmetic x/
FP Arithmetic x/
i. e. The product of two floating point numbers is
computed by multiplying the two mantissas and
using the sum of the two exponents as the
exponent of the product. The product will be
normalized and the exponent adjusted accordingly.
(f1 x 2 e1 ) * (f2 x 2 e2 ) = (f3 x 2e3 )
where f3 = f1 x f2, and e3 = e1+e2.
FP Arithmetic x/ Examples
(- 0.1403 10-4)
0.4238463 100
(+0.3021 105)
- 0.04238463 10-4+5
(- 0.9325 102)
109
(- 0.1002 10-6)
+ 9.306387 102-(-6)
+ 0.9306387