Uploaded by Breeze

CSAPP3eRecitation1

advertisement
Recitation (Chap 2.1-2.2)
1)
Perform the following number conversions:
A. 0x25B9D2 to binary
0010 0101 1011 1001 1101 0010
B. binary 1010111001001001 to hexadecimal
Group from right
1010 1110 0100 1001
Then we get
0x AE49
C. 0xA8B3D to binary
1010 1000 1011 0011 1101
D. binary 1100100010110110010110 to hexadecimal
Group from right
0011 0010 0010 1101 1001 0110
0x 322D96
2)
Consider the following three calls to show_bytes:
int a = 0x12345678;
byte_pointer ap = (byte_pointer) &a;
show_bytes(ap, 1); /* A. */
show_bytes(ap, 2); /* B. */
show_bytes(ap, 3); /* C. */
Indicate the values that will be printed by each call on a little-endian machine and on a bigendian machine:
Process:
Little Endian: 78 56 34 12
Big Endian: 12 34 56 78
A. Little endian:
78 Big endian: 12
prints 1 byte starting from the address pointed to by ap
B. Little endian:
78 56
Big endian: 12 34
prints 2 bytes starting from the address pointed to by ap
C. Little endian:
78 56 34 Big endian: 12 34 56
prints 3 bytes starting from the address pointed to by ap
3)
Suppose that a and b have byte values 0x55 and 0x46, respectively. Fill in the following table
indicating the byte values of the different C expressions:
a: 0101 0101
b: 0100 0110
Expression
a&b
a|b
~a | ~b
a & !b
Value
0x44
0x57
0xBB
0x00
Expression
a && b
a || b
!a || !b
a && ~b
Value
0x01
0x01
0x00
0x01
4)
Show that each of the following bit vectors is a two’s-complement representation of −4 by
applying Equation 2.3:
A. [1100]
-23+22=-4
B. [11100]
-24+23+22=-4
C. [111100]
-25+24+23+22=-4
Observe that the second and third bit vectors can be derived from the first by sign extension.
5)
Write a function with the following prototype:
/* Determine whether arguments can be added without overflow */
int uadd_ok(unsigned x, unsigned y);
This function should return 1 if arguments x and y can be added without causing overflow.
Here is my code and outcome in my VSCode
Download