Sunteți pe pagina 1din 19

Chapter 3.

4 – Data Representation, Structure and Manipulation

Binary Numbers

1. Convert denary number 117 to the corresponding binary number using an 8-bit register.

2 117
2 58 1
2 29 0
2 14 1
2 7 0
2 3 1
1 1

Answer: 01110101

2. Convert binary number 10110110 to the corresponding denary number.

Denary 128 64 32 16 8 4 2 1
Binary 1 0 1 1 0 1 1 0

Answer: 128 + 32 + 16 + 4 + 2 = 182

3. Convert denary number 117 to the corresponding octal number.

8 117
8 14 5
2 1 6

Answer: 165

4. Convert the octal number 2036 to the corresponding denary number.

Denary 2097152 262144 32768 4096 512 64 8 1


Octal 0 0 0 0 2 0 3 6

Answer: (512 * 2) + (64 * 0) + (8 * 3) + (1 * 6) = 1024 + 0 + 24 + 6 = 1054

5. Convert the denary number 2877 to the corresponding hexadecimal number.

16 2877
16 179 13
2

11 3

Answer: B3D

6. Convert the hexadecimal number 1E0A to the corresponding denary number.

Denary 268435456 16777216 1048576 65536 4096 256 16 1


Hexadecimal 0 0 0 0 1 E 0 A

Answer: (4096 * 1) + (256 * E) + (16 * 0) + (1 * A)


= 4096 + (256 * 14) + (16 * 0) + (1 * 10)
= 7690

Binary Coded Decimal (BCD)

Decimal BCD
0 0000
1 0001
2 0010
3 0011
4 0100
5 0101
6 0110
7 0111
8 1000
9 1001

7. Convert the denary number 438 to the corresponding BCD number.

Answer: 010000111000

8. Convert the BCD number 010100100111 to the corresponding denary number

Answer: 527

Two’s Complement

To convert a binary number to its two’s complement the following method is used:

1. The bits are written unchanged up to the first 1-bit.


3

2. Beyond the first 1-bit the 0-bits are changed to 1-bits and 1-bits are changed to 0-bits.

9. Convert the binary number +119 to the corresponding two’s complement

Binary number equivalent of +119 is 01110111

Then write down the above number from right, up to and including the first digit that is a 1.

Binary Number 0 1 1 1 0 1 1 1
2’s Complement 1 0 0 0 1 0 0 1

Therefore the two’s complement of 01110111 is 10001001

10. Find the denary number represented by the 2’s complement 11011001.

2’s Complement 1 1 0 1 1 0 0 1
2’s Complement 0 0 1 0 0 1 1 1
Positive denary 0 0 32 0 0 4 2 1

Therefore the positive denary number = 39


Answer = -39

Addition of Binary Numbers

11. Add the binary numbers 01011011 and 00010010.

First Number 0 1 0 1 1 0 1 1
Second Number 0 0 0 1 0 0 1 0
Answer 0 1 1 0 1 1 0 1
Remainder 1 1

Subtraction of Binary Numbers

12. Subtract the binary number 00010010 from the binary number 01011011.

First Number 0 1 0 1 1 0 1 1
Second Number 0 0 0 1 0 0 1 0
2’s complement 1 1 1 0 1 1 1 0
of the second
number
Answer 0 1 0 0 1 0 0 1

Answer: 64 + 0 + 0 + 8 + 0 + 0 + 1 = 73
Decimal Fractions and their Binary Representations

Fraction Decimal Fraction Binary Fraction


1/2 0.5 0.1
4

1/4 0.25 0.01


1/8 0.125 0.001
1/16 0.0625 0.0001
1/32 0.03125 0.00001
1/64 0.015625 0.000001
1/128 0.0078125 0.0000001
1/256 0.00390625 0.00000001
1/512 0.001953125 0.000000001
1/1024 0.0009765625 0.0000000001

Normalized Floating Point Representation

13. Using 8 bits for the mantissa and 8 bits for the exponent express 123 in normalized
floating point representation

Binary representation of 123 in 8 bits = 01111011


Normalized floating point representation of 123 = 0.1111011 x 2 7
Answer = 01111011 00000111

14. Using 8 bits for the mantissa and 8 bits for the exponent express -123 in normalized
floating point representation

Binary representation of 123 in 8 bits = 01111011


Two’s complement of 123 in 8 bits = 10000101
Normalized floating point representation of -123 = 1.0000101 x 2 7
Answer = 10000101 00000111

15. Using 8 bits for the mantissa and 8 bits for the exponent express 0.1875 in normalized
floating point representation

0.1875 = 0.125 + 0.0625


Binary representation of 0.1875 = 0.0011
Normalized floating point representation of 0.1875 = 0.11 x 2 -2
Binary representation of 2 in 8 bits = 00000010
Therefore the Two’s complement of -2 in 8 bits = 11111110
Answer = 01100000 11111110

16. Using 8 bits for the mantissa and 8 bits for the exponent express -0.1875 in normalized
floating point representation

Binary representation of 0.1875 in 8 bits = 0.0011000


Two’s complement of 0.1875 in 8 bits = 1.1101000
5

Normalized floating point representation of -0.1875 = 1.0100000 x 2 -2


Binary representation of 2 in 8 bits = 00000010
Therefore the Two’s complement of -2 in 8 bits = 11111110
Answer = 10100000 11111110

17. Using 8 bits for the mantissa and 8 bits for the exponent express 123.1875 in normalized
floating point representation

Binary representation of 123 = 01111011


Binary representation of 0.1875 = 0.0011
Binary representation of 123.1875 = 01111011.0011
= 0.111101100110000 x 2 7
= 0.1111011 x 2 7
= 0.1111011 x 2 00000111
Answer = 01111011 00000111

18. Using 8 bits for the mantissa and 8 bits for the exponent express -123.1875 in normalized
floating point representation

Binary representation of 123.1875 = 01111011.0011


Two’s complement of 123.1875 = 10000100.1101
Normalized floating point binary = 1.00001001101 x 2 7
Answer = 10000100 00000111

How the range and the accuracy of a number vary according to the number of bits in the
mantissa and the exponent?

When the number of bits in the mantissa increases the accuracy increases but the range decreases

Example: Suppose that a number is held in a 1-byte register.


When the mantissa is 8 bits it can hold the number 4.2378244 and the exponent can
hold the maximum of 01111111
Then the maximum number will be 4.2378244 x 2127

When the mantissa is 10 bits it can hold the number 4.237824458 and the exponent can
hold the maximum of 011111
Then the maximum number will be 4.237824458 x 231

When the number of bits in the exponent increases the accuracy decreases but the range increases

19. Convert the binary floating point representation 01101000 00000110 to its denary
equivalent.

Binary floating point 01101000 00000110 = 0.1101000 x 200000110


= 0.1101000 x 2 6
6

= 0110100.0
Denary equivalent of 01101000 00000110 = 52

Why it is not possible to represent zero as a normalized floating point number?

A normalized number must have the first two bits of the mantissa different. Therefore one of
those numbers must be a 1. Then the number will represent either -1 or 0.5 but not zero.

20. Represent the denary number 2 ½ in normalized floating point binary format using 5
bits for the mantissa and 3 bits for the exponent.

Binary representation of 2 ½ = 10.1


= 0.101 x 22
= 0.101 x 2010
Answer = 01010 010

21. Represent the denary number 4 ¾ in normalized floating point binary format using 5
bits for the mantissa and 3 bits for the exponent.

Binary representation of 4 ¾ = 100.11


= 0.10011 x 23
= 0.1001 x 2011
Answer = 01001 011

22. Represent the denary number -5/16 in normalized floating point binary format using 5
bits for the mantissa and 3 bits for the exponent.

5/16 = 1/4 + 1/16

Binary representation of 5/16 = 0.0101


Two’s complement of 5/16 = 1.1011
= 1.011 x 2 -1
= 1.011 x 2 11
Answer = 10110 111

23. Represent the denary number -5 13/16 in normalized floating point binary format using
5 bits for the mantissa and 3 bits for the exponent.

13/16 = 1/2 + 1/4 + 1/16


7

Binary representation of 13/16 = 0.1 + 0.01 + 0.0001


= 0.1101

Binary representation of 5 = 0101


Binary representation of 5 13/16 = 0101.1101
Two’a complement of 5 13/16 = 1010.0011

Floating point binary representation of -5 13/16 = 1.0100011 x 2 3


= 1.0100 x 2 011
= 1.0100 x 20 11
Answer = 10100 011

24. Using 5 bits for the mantissa and 3 bits for the exponent express -15/32 in normalized
floating point representation

+15/32 = 1/4 + 1/8 + 1/16 + 1/32


Therefore binary representation of +15/32 in 8 bits = 0.01 + 0.001 + 0.0001 + 0.00001
= 0.01111
Two’s complement of 15/32 in 8 bits = 1.10001
Normalized floating point representation of 15/32 = 1.0001 x 2 -1
= 1.0001 x 2 111
Answer = 10001 111

25. Using 8 bits for the mantissa and 8 bits for the exponent express 10 1/2 in normalized
floating point representation

Binary representation of 10 1/2 = 1010.1


Normalized floating point representation of 10 ½ = 0.10101 x 2 4
= 0.10101 x 2 100
Answer = 01010100 00000100

26. Using 8 bits for the mantissa and 8 bits for the exponent express -10 1/2 in normalized
floating point representation

Binary representation of 10 1/2 = 1010.1


Two’s complement of 10 1/2 = 10101.1
Normalized floating point representation of -10 ½ = 1.01011 x 2 4
= 1.01011 x 2 100
Answer = 10101100 00000100

Number Description Floating Point Binary Denary value


Representation

Largest Should have the largest 01111111 01111111 (1/2 + 1/4 + 1/8 + 1/16 + 1/32 +
magnitude positive mantissa and 1/64 + 1/128) x 2127
8

positive the largest positive


number exponent = 127/128 x 2127

Smallest Should have the 01000000 10000000 1/ 2 x 2-128


magnitude smallest positive
positive mantissa and the largest
number negative exponent.

Largest Should have the largest 10000000 01111111 -1 x 2127


magnitude magnitude negative
negative mantissa and the largest
number positive exponent
occurs.

Smallest Should have the 10111111 10000000 -(0 + 1/4 + 1/8 + 1/16 + 1/32 + 1/64
magnitude smallest magnitude + 1/128) x 2-128
negative negative mantissa and
number the largest negative = - (1 – 63/128) x 2-128
exponent occurs.
= -65/128 x 2-128

Static data structures

This is a data structure that does not change in size while the program is running. An example of
a static data structure

Dynamic data structure


9

This is a data structure that can increase or decrease in size while a program is running. An
example of a dynamic data structure is a linked list.

Data Examples Advantages Disadvantages


structure
Static Array 1. Easy to program 1. Programmer has to
estimate the maximum
amount of space that
is going to be needed

2. An array allows for random 2. Can waste a lot of


Access memory space

Dynamic Linked Lists 1. Only uses the memory space needed at 1. Difficult to program
Stack that particular time
Queue
Binary tree 2. Does not produce overflow 2. A linked list only
allows serial access
and does not allow
random access

3. Makes efficient use of Memory 3. Can be slow to


implement searches

4. It is easier to insert and delete elements


from a linked list rather than an array
because it involves only the deleting
and creating of few pointers

5. Storage no longer required can be


returned to the system for other uses

Algorithms involving Linked Lists

Algorithm 1: Insertion of a data item to a sorted linked list

Before the data was inserted


10

START Adam Bob David Edward LINKED LIST

NEXTFREE FREE LIST

After the data was inserted

First data Previous data Current data Next data


START Adam Bob David Edward LINKED LIST

New data
NEXTFREE Colin FREE LIST

IF free list is empty THEN


OUTPUT error message
ELSE
store the new data in the node pointed to by the NEXTFREE pointer
READ data from the linked list following the pointers
IF new data = < current data
IF current data <> first data
change the pointer of the previous data to point to the new data
change the pointer of the new data to point to the current data
ELSEIF current data = first data
set the pointer of the new data to point to the current data
set START pointer to point to the new data
ENDIF
ELSEIF End Of List has reached and new data > = current data
set the pointer of current data to new data
set the pointer of new data to NULL
ENDIF
set NEXTFREE pointer to point to the next free location
ENDIF

Algorithm 2: Deleting an item from a linked list

REPEAT
READ data from the linked list by following the pointers
IF current data = data to be deleted THEN
11

set the pointer of the previous data to the next data


add the current data to the free list and
ENDIF

Algorithm 3: Searching for an item in a linked list

REPEAT
READ data from the linked list by following the pointers
IF current = data searched THEN
DISPLAY message “A match is found”
STOP
ENDIF
UNTIL end of the list
IF current data <> data searched THEN
DISPLAY message “A match is not found”
STOP
ENDIF

Search Algorithms

 Serial search is carried out by continually comparing the desired key with each key in the list
until a match is found or the end of the list is reached.

 Binary search is carried out by continually comparing the desired key with the middle key of
the list and hence repeatedly splitting the list into two equal halves until the desired key
equals to the middle key or until the list is empty.

 Serial search is useful if the list is not sorted and if the number of records in the list is small.

 Binary search is faster if the list is sorted and if the number of records in the list is large

Algorithm 4: Searching for an item in a list using serial search

Serial search is carried out on a list of data by comparing each key with the one being searched,
until a match is found or the end of the file is reached.

BEGIN
12

REPEAT
READ the key of the current record
IF the current key = desired key THEN
DISPLAY message “Item found”
STOP
ENDIF
UNTIL end of list
IF current key <> the desired key THEN
DISPLAY message “Item not found”
STOP
ENDIF
END

Algorithm 5: Searching for an item in a sorted list using binary search

BEGIN
REPEAT
READ the key of the middle record of the list
IF the key of the middle record = desired key THEN
DISPLAY message “Item found”
STOP
ELSE
Split the list into two halves
IF the key of the middle record < desired key THEN
Consider the second half of the list
ELSEIF
Consider the first half of the list
ENDIF
ENDIF
UNTIL the number of items in the list = 1
IF the key of the record left = desired key THEN
DISPLAY message “Item found”
STOP
ELSE
DISPLAY message “Item not found”
ENDIF

Stack
Stack is a LIFO (last-in first-out) data structure.
An item is both pushed-on and popped-off from the top of the stack.

Queue
13

Queue is a FIFO (first-in first-out) data structure.


An item is inserted to the queue from the rear and deleted from the front.

Algorithms involving Stacks and Queues

A problem that can occur when a program tries to insert a new value to a stack or a queue which
is represented as an array is the situation where the array is full. The stack pointer helps to
recognize this situation by pointing to the outside of the array.

A problem that can occur when a program tries to read a value from a stack or a queue which is
represented as an array is the situation where the array is empty. The stack pointer helps to
recognize this situation by pointing to the null value.

Algorithm 6: Insertion of an item to a stack (when the stack is represented by an array)

Memory addresses increase in this direction

Data Stack Pointer (points to the top of the stack)


Data
Data
Data

IF stack is full
DISPLAY message “Stack is full.”
STOP
ELSE
Increment stack pointer by 1
Store the new data at the location pointed to by the stack pointer
STOP
ENDIF

Algorithm 7: Deletion of an item from a stack (when the stack is represented by an array)

Memory addresses increase in this direction

Data Stack Pointer (points to the top of the stack)

Data
Data
Data
14

IF stack is empty
DISPLAY message “Stack is empty”
STOP
ELSE
Set data in the cell pointed to by the stack pointer to the variable item_deleted
Decrement the stack pointer by 1
STOP
ENDIF

Algorithm 8: Insertion of an item to a queue (when the queue is represented by an array)

If the Head pointer points to the front of the queue and the tail pointer points to the back of the
queue:

Memory addresses increase in this direction

Data Tail Pointer (points to the back of the queue)


Data
Data
Data Head Pointer (points to the front of the queue)

IF queue is full
DISPLAY message “Queue is full.”
STOP
ELSE
Increment tail pointer by 1
Store the new data at the location pointed to by the tail pointer
STOP
ENDIF

Algorithm 9: Deletion of an item from a queue (when the queue is represented by an array)

If the Head pointer points to the front of the queue and the tail pointer points to the back of the
queue:
15

Memory addresses increase in this direction

Data Tail Pointer (points to the back of the queue)


Data
Data
Data Head Pointer (points to the front of the queue)

IF queue is empty
DISPLAY message “Queue is empty”
STOP
ELSE
Set data in the cell pointed to by the head pointer to the variable item_deleted
Increment the head pointer by 1
STOP
ENDIF

Algorithms involving Binary Trees

Algorithm 10: Inserting an item (X) to a binary tree

BEGIN
REPEAT
READ X
READ root node
WHILE node NOT EMPTY DO
IF X < value at node
Follow left sub-tree
ELSE
Follow right sub-tree
ENDIF
ENDWHILE
INSERT X at last read node
UNTIL end of list
END

Example

Write the following data to a binary tree

Michael, Jack, Victor, John, Sam, Nathan, Edward, Harry, Lisa, Xavier, Mark, Pam, Ivan, Frank,
Walter, Izack, Colin, Kevin, Edwin, Zev, Jessie, Irene, Gary, Miranda
16

Michael

Jack Victor

Edward John Sam Xavier

Colin Harry Jessie Lisa Nathan Walter Zev

Frank Ivan Kevin Mark Miranda Pam

Edwin Gary Irene Izack

Algorithm 11: Reading a binary tree data structure in alphabetic order

BEGIN
Traverse left sub-tree
READ the node
Traverse right sub-tree
READ the node
END

Example:

Read the following binary tree in alphabetical order

Michael (16)
17

Jack (10) Victor (21)

Edward (2) John (12) Sam (20) Xavier (23)

Colin (1) Harry (5) Jessie (11) Lisa (14) Nathan (18) Walter (22) Zev (24)

Frank (4) Ivan (8) Kevin (13) Mark (15) Miranda (17) Pam (19)

Edwin (3) Gary (6) Irene (7) Izack (9)

Algorithms involving Sorting of Lists

The three sorting algorithms are:

Bubble Sort algorithm


Insertion Sort algorithm
Quick Sort algorithm

Quick Sort algorithm is the fastest in sorting a list and Insertion Sort algorithm is faster than the
Bubble Sort algorithm.

Algorithm 12: Sorting a list in ascending order using the bubble sort algorithm

Bubble sort algorithm sorts a given unsorted list of items in ascending order.

DO
FOR pass IN RANGE OF 1 to (N – 1) DO
FOR c IN RANGE OF 1 TO (N – pass) DO
IF Data (c) > Data (c + 1) THEN
SET Temp TO Data (c)
SET Data (c) to Data (c + 1)
SET Data (c + 1) TO Temp
ENDIF
ENDFOR
ENDFOR

Example:

Explain how the bubble sort algorithm is used to sort the list 12, 34, 23, 5, 78, 10 in ascending
order:

pass Sequence
18

1 12 34 23 5 78 10
12 34 23 5 78 10
12 23 34 5 78 10
12 23 5 34 78 10
12 34 5 34 78 10
2 12 34 5 23 10 78
12 34 5 23 10 78
12 5 34 23 10 78
12 5 23 34 10 78
12 5 23 10 34 78
3 12 5 23 10 34 78
5 12 23 10 34 78
5 12 23 10 34 78
5 12 10 23 34 78
5 12 10 23 34 78
4 5 12 10 23 34 78
5 12 10 23 34 78
5 10 12 23 34 78
5 10 12 23 34 78
5 10 12 23 34 78

Algorithm 13: Sorting a list in ascending order using the insertion sort algorithm

First value of the given unsorted list is written to the new list
Starting from the second value in the given unsorted list
write each value to the new list
by inserting it to the correct place of the new list
by moving appropriate values to the right to create space
Repeat until the end of the given unsorted list

Algorithm 14: Sorting a list in ascending order using the quick sort algorithm

Split the list into two sublists


Set up two pointers (one at each end of the list) called left pointer and right pointer
REPEAT
Quick sort the list by comparing the two numbers pointed to by the pointers and swap if
necessary
UNTIL the pointers coincide
Quick sort each sublist until all the sublists contain just a single data in which case the entire list
is sorted

Algorithm 15: Merging of two sorted lists

Select one value from each list


WHILE neither list is empty
Compare the values in the lists
Copy the smaller value to the new list
Read the next value in the list that has a value copied
19

ENDWHILE
When one list is empty copy the remaining values of the other list to the new list

Explain how storing data in ascending order within an array would affect the management
of storage and the search for a given data.

1. When a student leaves then all students higher in the alphabet have to be moved up one place
in the array

2. When a new student arrives, the correct location in the array needs to be found and the
remaining students moved down one place

3. A binary search may be used involving looking at the centre value in the remaining part of the
array at each pass and then cutting number of items to be searched in half

4. Speeds up any searches that have to be made (because of reduced number of comparisons)

S-ar putea să vă placă și