Tutorial #01: C and Number Systems
Week: 03
Post on the LumiNUS forum if you have any queries.
Question 1
In 2s complement representation, "sign extension" is used when we want to represent an n-bit signed integer as an m-bit signed integer, where m > n. We do this by copying the sign-bit of the n-bit signed integer m - n times to the left of the n-bit number ot create an m-bit number.
So for example, if we want to sign-extend (0110)2 to an 8-bit number. Here, n = 4 and m = 8. Thus, we copy the sign m - n = 4 times, giving us (00000110)2.
Similarly, if we want to sign-extend (1010)2 to an 8-bit number, we would get (11111010)2.
Show, that IN GENERAL sign extension is value-preserving. For example, (00000110)2 = (0110)2 and (1010)2 = (11111010)2.
Question 2
We generalise (r-1)s complement (also called radix diminished complement) to include fraction as follows:
(r-1)s complement of N = rn - r-m - N
where n is the number of integer digits and m is the number of fractional digits. (If there are no fractional digits, then m = 0 and the formula becomes rn - 1 - N as given in the class.)
For example, the 1s complement of (011.01)2 is:
rn - r-m - N
= (23 - 2-2) - (011.01)2
= ((1000)2 - (0.01)2) - (011.01)2
= (111.11)2 - (011.01)2
= (100.10)2
Perform the following binary subtractions of values represented in 1s complement representation by using addition instead. (Note: Recall that when dealine with complement representations, the two operands must have the same number of digits.)
Is sign extension used in your working? If so, highlight it.
Check your answers by converting the operatnds and answers to their actual decimal values.
- (0101.11)2 – (010.0101)2
- (010111.101)2 – (0111010.11)2
Question 3
Convert the following decimal numbers to fixed-point binary in 2s complement, with 4 bits for the integer portion and 3 bits for the fraction portion:
- 1.75
- -2.5
- 3.876
- 2.1
Using the binary representations you have just derived, convert them back into decimal. Comment on the compromise between range and accuracy of the fixed-point binary system.
Question 4
Past-Year
This question appears in AY2010/2011 Semester 2 Term Test #1.
How would you represent the decimal value -0.078125 in IEEE 754 single-precision representation? Express your answer in hexadecimal. Show your working.
Question 5
Given the partial C program shown below (Tut1Q5.c
), complete the two functions:
readArray()
: Read data into an integer array (with at most 10 elements).reverseArray()
: Reverse the array.
For reverseArray()
, you are to provide two versions:
- An iterative version.
- A recursive version.
For the recursive version, you may write an auxiliary/drive function to call the recursive function.
Tut1Q5.c | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
|
Question 6
Trace the following program (Tut1Q6.c
) manually (do not run it on a computer) and write out its output.
When you present your solution, draw diagrams to explain.
Tut1Q6.c | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
|