CS223: Systems Programming - Practice Quiz 2
Name:
Complete the following exercises in 50 minutes. Practice quizzes are closed book. For actual quizzes you may bring a cheat sheet.
Question 1: Short answers
1.1 Convert the following binary numbers to hexadecimal
-
0b 0110 1010
-
0b 1110 0011
-
0b 0110 1110
-
0b 0011 1101
1.2 Convert the following hexadecimal numbers to binary
-
0xF5
-
0x2D
1.3 Give formulas that would convert the following numbers to decimal
-
0xF5
-
0b1001
-
0644
1.4 Show how you would use signed extension to assign the value 0xF5 to a 4 byte data type. Give your answer in hexadecimal.
1.5 Suppose the value 0x11223344 is stored at address 0xfeee4e6c. Show how the bytes would be ordered on a little endian machine.
1.6 Compute the following values. Assume 4-bit values. Give your final answer in hexadecimal.
-
3 << 8
-
2 & 7
-
-6 >> 3
-
3 ^ E
1.7 Draw the contents and layout of a variable of this struct type, assuming the variable is placed in memory starting at address 0xfffffe40.
struct foo {
float x;
unsigned char c[3];
struct foo* v;
};
Question 2
Consider the following program. Answer the following questions
-
At the line indicated by the comment, draw the stack and heap diagram for the program
-
What is the minimum number of bytes allocated for the variable
datainmain()?
-
What is the minimum number of bytes allocated for the variable
newNodeininsert()?
-
Does this program have a memory leak? Briefly explain why or why not.
struct mystery {
float x;
float y;
};
struct node {
int val;
struct mystery* m;
struct node* next;
};
struct node* insert(int val, struct node* head, struct mystery* m) {
struct node* newNode = malloc(sizeof(struct node));
assert(newNode != NULL); // abort program on error
newNode->val = val;
newNode->m = m;
newNode->next = head;
return newNode;
}
int main() {
struct mystery* data = malloc(sizeof(struct mystery));
assert(data != NULL); // abort program on error
data->x = -2.5;
data->y = 1.5;
struct node* head = NULL;
insert(3, head, data);
// draw the stack here
free(head);
free(data);
return 0;
}
Question 3
Consider the following program
1 #include <stdio.h>
2
3 int main() {
4 unsigned char a = 0x7E;
5 unsigned char leftMask = 0xC0;
6 unsigned char rightMask = 0x03;
7 unsigned char left = (leftMask & a) ;
8 unsigned char right = (rightMask & a) ;
9 unsigned char leftShift = left >> 4;
10 unsigned char rightShift = right << 4;
11 unsigned flipped = leftShift | rightShift;
12
-
What is the value of
ain binary?
-
What is the value of
flipped? Show your work to give a rationale for your answer.
Question 4
Consider the following file permissions.
-rw-r--r-- 1 alinen alinen 191 Sep 10 12:07 greeting.c
-rwxr-xr-x 1 alinen alinen 15960 Sep 3 12:33 hello
-rw-r--r-- 1 alinen alinen 73 Sep 3 12:26 hello.c
-
What octal number corresponds to the file permissions for
hello.c?
-
What octal number corresponds to the file permissions for
hello?
-
Suppose we want to change the permissions for
helloso that groups have read/write access. What octal number would correspond to the new permission?
Question 5
Suppose we have a 4-bit bit field that stores whether an animal can fly, swim, or walk, defined with the following masks
#define NONE 0x0
#define FLY 0x1
#define SWIM 0x2
#define WALK 0x4
-
Suppose an animal’s bitfield contains the value 0x3. What capabilities does it have?
-
What is the minimum sized data type that can hold this bitfield?
-
What value would the bitfield have for an otter, which can swim and walk?
-
Write C code that tests whether an animal can walk or fly.
Question 6
Consider the floating-point value 0x 3f c0 00 00.
A) What is the above number in binary?
B) Using your answer from part A, label which bits represent the sign, the exponent, and the significant bits.
C) What decimal value does the above number correspond to? Show your work.