CS 370 Homework #2

This 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.
  1. (4 points) Convert the following hexadecimal numbers to binary:
  2. (4 points) Convert the following binary numbers to hexadecimal:
  3. (4 points) Convert each of the binary numbers from the previous problem to decimal; show your work. Assume they represent unsigned integers.

  4. (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 hexadecimal.

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.

  1. (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.

  2. (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.
  3. (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?
  4. (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')