printable version
Test 2
[1]
[2]
[3]
[4]
[5]
[6]
[7]
[8]
[9]
[10]
Problem X2.1.
[12 pts]
Suppose we have declared a C struct as below
for representing a linked list's node.
struct node {
int value;
struct node *next;
};
Complete the following function so that finds the first
node whose value is find and inserts a new node following
it containing insert. Thus if our list contains three
nodes whose values are 2, 3, 5, and we are told to find 3 and insert 4
after it, then after calling the function the list would
containing 2, 3, 4, 5.
You may assume that find is actually present in the list.
If find appears multiple times, then you would want to
insert after just the first occurrence of find.
void list_insert_after(struct node *head, int find, int insert) {
void list_insert_after(struct node *head, int find, int insert) {
struct node *cur;
struct node *after;
while (cur->value != find) {
cur = cur->next;
}
after = (struct node*) malloc(sizeof(struct node));
after->value = insert;
after->next = cur->next;
cur->next = after;
}
Problem X2.2.
[6 pts]
For the following, assume a six-bit two's-complement
representation of integers.
| a. | What numeric value does 110110
represent? |
| b. | What bit pattern is used to represent
−12(10)? |
Problem X2.3.
[6 pts]
What is the value of each of the following C expressions?
Express your answers in hexadecimal.
| a. | 0x11 ^ 0x3 |
| b. | 0xC000100 >> 3 |
Problem X2.4.
[8 pts]
Suppose we number the bits of an integer from 0 to 31.
Complete the function set_bits so that it returns a number
with all bits with indices between lo and hi,
inclusive.
For example, set_bits(2, 5) would return the number whose
binary representation is 00…00111100.
Your solution should use only variable declarations,
variable assignments, and a return statement.
You may assume that both lo and hi are between 0
and 31 (inclusive).
int set_bits(int lo, int hi) {
int set_bits(int lo, int hi) {
return ~(-2 << hi) ^ (-1 << lo);
}
Alternatively we could return (1 << (hi + 1)) - (1 << lo);
one potential problem is that this won't necessarily work
when hi is 31.
Problem X2.5.
[8 pts]
Draw the logic circuit corresponding most closely to the Boolean expression
w (x + x y).
Problem X2.6.
[6 pts]
Using DeMorgan's law, rewrite
x + y without using
addition.
Problem X2.7.
[6 pts]
Given the below Karnaugh map, identify the corresponding minimized
Boolean expression.
w x y + x z + x y
Problem X2.8.
[8 pts]
Draw a Karnaugh map corresponding to the following truth table,
and derive a minimized Boolean expression for the function.
| x |
y |
z |
out |
| 0 | 0 | 0 | 1 |
| 0 | 0 | 1 | 0 |
| 0 | 1 | 0 | 1 |
| 0 | 1 | 1 | 0 |
| 1 | 0 | 0 | 0 |
| 1 | 0 | 1 | 1 |
| 1 | 1 | 0 | 1 |
| 1 | 1 | 1 | 0 |
x z + y z + x y z
Problem X2.9.
[8 pts]
Below, draw a circuit taking two 2-bit inputs a and
b (whose individual bits are
a1a0 and
b1b0)
as well as a 1-bit input op.
The output of the circuit should be a 3-bit number that is
a + a if op is 0
and a + b if op is 1.
Feel free to use components we saw in class, such as adders,
multiplexers, demultiplexers, and flip-flops.
Problem X2.10.
[12 pts]
Consider the 8-bit floating-point representation we studied in
class, including support for denormalized numbers and nonnumeric
values. It used 3 bits for the mantissa and 4 bits for the
excess-7 exponent.
| a. | What 8-bit pattern represents the number
0.125(10)? |
| b. | What base-10 integer or fraction does the
bit pattern 11001100 represent? |
| c. | What base-10 integer or fraction does the
bit pattern 00000010 represent? |
| d. | What bit pattern results from multiplying 240
and −240? |
| a. | 00100000 |
| b. | −6 |
| c. | 1/256 |
| d. | 11111000 |