a. Convert each of the unsigned decimal values below into its corresponding binary value
(w = 8), then convert the binary value into its corresponding hexadecimal value.
I. 15710
II. 24810
b. Convert each of the signed decimal values below into its corresponding two’s
complement binary value (w = 8), then convert the binary value into its corresponding
hexadecimal value.
I. 12310
III. -7410
c. Interpret each of the binary values below first as an unsigned decimal value, then as a
signed decimal value (using the two’s complement encoding scheme).
I. 111010012
II. 100101102
d. Convert 24710 into a signed value directly, without converting it first to its
corresponding binary value (w = 8).
e. Convert -15210 into a unsigned value directly, without converting it first to a binary
number (w = 8).
2. [6 marks] Unsigned and signed arithmetic operations and overflow
For a. below, convert each of the operands (unsigned decimal values) into its corresponding
binary value (w = 8).
For b. below, convert each of the operands (signed decimal values) into its corresponding
two’s complement binary value (w = 8).
For a. and b. below, perform both the decimal addition and the binary addition and indicate
the true sum and the actual sum and whether they are the same or different.
For the binary addition, clearly label all carry in bits (by using the label “carry in”) and the
carry out bit (by using the label “carry out”).
Finally, indicate whether or not an overflow occurred (for signed values, specify whether
the overflow is positive or negative). If an overflow occurred, explain how addition overflow
can be detected …
1. at the bit level, and
2. using the decimal operands.
a. Unsigned addition:
I. 7410 + 6310
II. 12310 + 15710
b. Signed (two’s complement) addition:
I. 2810 + -7410
II. -11710+ 12610
III. 7410 + 6310
IV. -11910 + -10510
3. [8 marks] C Code, endian and bit-level manipulation
Download and extract Assn1-files and open Assn1_Q3.c, Assn1_main.c and makefile in a
text editor. Read and understand their content. Using the makefile, compile and execute
the program.
Requirements:
While answering this question, you must not change the prototype of the functions
given. The reason is that these functions will be tested using a test driver built based
on these function prototypes.
a. Modify the printf statement of the show_bytes(…) function such that it first
prints the memory address of each byte then the content of the byte itself. Here is an
example:
0x7ffe5fb887cc 0x80
where 0x7ffe5fb887cc is the memory address of a byte which contains the value
0x80. Compile and test your program.
b. Looking at the output of this program, would you say that the CSIL computer you are
using is a little endian or a big endian computer? Justify your answer by including some
of the output of your program in your answer.
c. Modify the loop of the show_bytes(…) function such that, instead of using array
notation to access each element of the array start, it uses pointer notation to access
each of these elements. The output of your program should remain the same as in a.
above.
d. Write a function called show_bits( … ). This function must have the following
prototype:
void show_bits(int);
This function must print the bit pattern of the parameter of type int. Compile and test
your program. Here are two test cases (data and expected results) to illustrate the
behaviour of this function:
Test Case 1: If the parameter (int) is 12345, then show_bits( … ) prints:
00000000000000000011000000111001
Test Case 2: If the parameter (int) is -12345, then show_bits( … ) prints:
11111111111111111100111111000111
Add this function to Assn1_Q3.c.
e. Write a function called mask_LSbits( … ). This function must have the following
prototype:
int mask_LSbits( int n );
This function creates (returns) a mask with the n least significand bits set to 1. For
example, if n is 2, the function returns 3 (i.e., 0x00000003) and if n is 15, the
function returns 32767 (i.e., 0x00007fff). What happens when n >= w or when n <=
0? When n >= w, your function must return a mask of all 1’s. When n <= 0, your
function must return a mask of all 0’s, i.e., 0.
Requirements (for e.):
o In creating this function, when you create the mask, you cannot use division,
modulus, multiplication, conditional or iterative statements (such as loops) or
call other function(s).
o You can use conditional statements in your function when you validate the
parameter.
Add this function to Assn1_Q3.c.
Testing: For part e., make sure you test your code with valid and invalid test cases.
o Here is an example of an valid test case: calling mask_LSbits(4).
o Here is an example of an invalid test case: calling mask_LSbits(0).
In order to test your program, you can put your test cases in Assn1_main.c.
Reviews
There are no reviews yet.