Documente Academic
Documente Profesional
Documente Cultură
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
Denary 128 64 32 16 8 4 2 1
Binary 1 0 1 1 0 1 1 0
8 117
8 14 5
2 1 6
Answer: 165
16 2877
16 179 13
2
11 3
Answer: B3D
Decimal BCD
0 0000
1 0001
2 0010
3 0011
4 0100
5 0101
6 0110
7 0111
8 1000
9 1001
Answer: 010000111000
Answer: 527
Two’s Complement
To convert a binary number to its two’s complement the following method is used:
2. Beyond the first 1-bit the 0-bits are changed to 1-bits and 1-bits are changed to 0-bits.
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
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
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
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
13. Using 8 bits for the mantissa and 8 bits for the exponent express 123 in normalized
floating point representation
14. Using 8 bits for the mantissa and 8 bits for the exponent express -123 in normalized
floating point representation
15. Using 8 bits for the mantissa and 8 bits for the exponent express 0.1875 in normalized
floating point representation
16. Using 8 bits for the mantissa and 8 bits for the exponent express -0.1875 in normalized
floating point representation
17. Using 8 bits for the mantissa and 8 bits for the exponent express 123.1875 in normalized
floating point representation
18. Using 8 bits for the mantissa and 8 bits for the exponent express -123.1875 in normalized
floating point representation
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
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.
= 0110100.0
Denary equivalent of 01101000 00000110 = 52
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.
21. Represent the denary number 4 ¾ in normalized floating point binary format using 5
bits for the mantissa and 3 bits for the exponent.
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.
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.
24. Using 5 bits for the mantissa and 3 bits for the exponent express -15/32 in normalized
floating point representation
25. Using 8 bits for the mantissa and 8 bits for the exponent express 10 1/2 in normalized
floating point representation
26. Using 8 bits for the mantissa and 8 bits for the exponent express -10 1/2 in normalized
floating point 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
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
This is a data structure that does not change in size while the program is running. An example of
a static data structure
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.
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
New data
NEXTFREE Colin FREE LIST
REPEAT
READ data from the linked list by following the pointers
IF current data = data to be deleted THEN
11
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
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
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
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.
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)
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
If the Head pointer points to the front of the queue and the tail pointer points to the back 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
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
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
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
BEGIN
Traverse left sub-tree
READ the node
Traverse right sub-tree
READ the node
END
Example:
Michael (16)
17
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)
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
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)