Download CAS CS 210 - Computer Systems

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Computational complexity theory wikipedia , lookup

Multiple-criteria decision analysis wikipedia , lookup

Transcript
CAS CS 210 - Computer Systems
Spring 2016
Answers to Problem Set #1 (Logic, Data Representation & Arithmetic)
Due: Friday, February 12, 1:00 pm
To be completed individually. For all questions, show your work in obtaining the
final answers.
Total score: 100 points
1. Solve problem 2.60, on page 128, from our CMU text.
Answer: (10 points)
1 unsigned replace_byte (unsigned x, int i, unsigned char b)
2 {
3 int itimes8 = i << 3;
4 unsigned mask = 0xFF << itimes8;
5
6 return (x & ~mask) | (b << itimes8);
7 }
2. Solve problem 2.61, on page 129, from our CMU text.
Answer: (4*3=12 points)
A. !!x
B. !!~x
C. !!(x & 0xFF)
D. !!(~x & (0xFF << ((sizeof(int)-1)<<3)))
3. Solve problem 2.64, on page 130, from our CMU text.
Answer: (10 points)
1 /* Return 1 when any odd bit of x equals 1; 0 otherwise. Assume w=32 */
2 int any_odd_one(unsigned x) {
3 /* Use mask to select odd bits */
4 return (x&0xAAAAAAAA) != 0;
5 }
1
4. Solve problem 2.71, on page 132, from our CMU text.
Answer: (5+8=13 points)
A. The function does not perform any sign extension. For example, if we attempt to extract
byte 0 from word 0xFF, we will get 255, rather than -1.
B. The following code uses a well-known trick for using shifts to isolate a particular range of
bits and to perform sign extension at the same time. First, we perform a left shift so that
the most significant bit of the desired byte is at bit position 31. Then we right shift by 24,
moving the byte into the proper position and performing sign extension at the same time.
1 int xbyte(packed_t word, int bytenum)
2 {
3 int left = word << ((3-bytenum) << 3);
4 return left >> 24;
5 }
5. Solve problem 2.74, on page 134, from our CMU text.
Answer: (10 points)
1 /* Determine whether arguments can be subtracted without overflow */
2 int tsub_ok(int x, int y) {
3 int diff = x-y;
4 int neg_over = x < 0 && y >= 0 && diff >= 0;
5 int pos_over = x >= 0 && y < 0 && diff < 0;
6 return !neg_over && !pos_over;
7 }
6. Convert the following numbers as indicated, using as few digits in the results as necessary.
(a) (010101)2 to base 10
(b) (011100)2 to base 16
(c) (54.125)10 to binary
(d) (122.3)4 to base 16
Answer: (4*3=12 points)
(a) 21; (b) 1C; (c) 110110.001; (d) 1A.C
2
7. Show the results of adding the following pairs of five-bit two’s complement numbers and
indicate whether or not overflow occurs for each case.
(a) 11110 + 11101
(b) 10111 + 10111
(c) 11111 + 01011
Answer: (3*4=12 points)
(a) 11011 (No overflow); (b) 01110 (Overflow); (c) 01010 (No overflow)
8. Assuming a 64-bit machine and two’s complement representation for integers, show the total
number of bits used, and all the bit values, for the following variables:
char c = 35;
char d = ’G’;
int x = -42;
Answer: (3*3=9 points)
c: 0010 0011
d:
0100 0111
// ascii code of ’G’
x: 0x FF FF FF D6 // two’s complement representation
3
9. Complete the following code to perform the conversion from ASCII bit representation for the
variable n to the magnitude-only bit representation for the variable x. You may assume that
the user enters exactly three digits for the input. (Hint: what are the magnitude-only values
for the bit patterns that represent the ASCII numeric symbols?)
Answer: (12 points)
#include <stdio.h>
main()
{
char n[4];
int x;
int i;
printf("Enter a 3-digit non-negative number: ");
scanf("%s", n);
x = 0;
for (i=0; i<3; i++) {
x = 10*x + (n[i]-48);
}
printf("The number is %i \n", x);
}
4