CS 370 Homework #2
assignment is worth 50 points.
Reading. CSAPP, Chapter 2,
Sections 2.1 - 2.3
Written Problems: Due 9/29/17 IN CLASS (24 points)
Note: the Practice Problems in each
chapter are your friends. Look at them, try those you aren't sure about; the
answers are at the end of the chapter.
Show as much of your work as possible.
- (4 points) Convert the following hexadecimal numbers to binary:
- (4 points) Convert the following binary numbers to hexadecimal:
- 1111 1000
- 0100 1010 1101 0010
- 0010 0010 0010 1011
- 1101 1110 1110 1101 1011 1101 1101 1111
(4 points) Convert each of the binary numbers from the previous
problem to decimal; show your work. Assume they represent unsigned
- (12 points) Perform the three shift operations given below on each of
the binary numbers given in Problem 2 above. Show the final answer in
- x << 2
- x >> 3 (logical)
- x >> 3 (arithmetic)
Programming Problems: Due 9/28/17 by 11:59 pm; hardcopy printouts in
class (26 points)
E-mail your source code to the
instructor by the due date/time (as attachments, please); also print
the source and turn in with your written work.
Each problem below will be a separate C/Java file.
- (4 points) Write a C function called
isNegative to determine if a
signed integer (2's complement number) is positive or negative, using
bitwise operators such as shift (no if statements!). Write a test
program for the function.
- (6 points) EXERCISE 2.60. Write this as a function along with a
complete program to test the function with at
least 3 different inputs.
- (4+4 points) EXERCISE 2.61. First try writing these with no
restrictions on the code (you can use conditionals, equality tests,
etc.) Then try writing equivalent single-line expressions using the
bit-level integer coding rules. Hint: what is !!x?
- (8 points) Write a Java program that translates a single
hexadecimal numbers to decimal. Your program will read the number to be
converted from the command line and output either ERROR (if the number
is not valid hexadecimal) or the decimal equivalent. This means that the
input will be treated as a String!
Include a screenshot showing the results of running the program on the
values given in Written Problems #1 (but leave off the prefix 0x).
A fast way to convert a character ch
holding a digit '0' through '9' to an integer is: (int)
(ch - '0')