## 1. Due Date

HW 2 due: : Due by noon, Wednesday March 3, 2021

Written assignments are not worth as much as lab assignments, but are designed to give you some extra practice on lecture material and on some of the type of questions you may see on course exams.

You are required to work with your assigned Homework and Study Group on homework assignments in this class. Please read this information about our requirements for working in your study groups, group roles, and tips for working on joint homework assignments with your study group: About CS31 Homework and Study group.

You will submit just one joint solution by your Homework and Study group to gradescope (see the link to the assignment below). Be sure that all students in your group are included in your gradescope group submission, and be sure to take turns in different group roles each HW assignment (see the link above for more information). The homework assignment will include a section that you fill in specifying who served in each role.

How to use Gradescope is a short video stepping you through how to use gradescope and how to add group partners to create your joint Homework Group gradescope submission.

## 3. The Assignment

Answer the questions below showing your work and/or explaining your answer. For these questions, if a question specifies a number of bits, your answer should be in a corresponding number of digits. For example, if the question asks to add 4 bit values together your answer should be a 4 bit value, not a 64 bit one. Also, assume that for signed values, the high-order bit is the sign bit. For example, `1000` should be interpreted as negative as a 4-bit signed value, but positive as an 8-bit signed value `(00001000)`.

1. What is the largest positive value that can be represented with a 2’s complement 8 bit number? Explain.

2. Convert the unsigned 8 bit binary value `10110000` to decimal. Show your work.

3. Convert the signed 8 bit binary value `10110000` to decimal. Show your work.

4. For the following 8 bit binary values (show your work):

```value 1: 01010101
value 2: 01100100```
1. What is the binary representation of the result of adding them together (show your work)?

2. Does this operation result in overflow? If so, when (i.e., under what circumstances / interpretations of the values)?

3. What is the decimal representation of the resulting addition if the two values are signed 8 bit values? (show your work)

4. What is the decimal representation of the resulting addition if the two values are unsigned 8 bit values? (show your work)

5. For the following 8 bit binary values (show your work):

```value 1: 01010101
value 2: 01100100```
1. What is the binary representation of the result of subtracting the second from the first (show your work)?

2. Does this operation result in overflow? If so, when? explain.

3. What is the decimal representation of the resulting subtradction if the two values are signed 8 bit values? (show your work)

4. What is the decimal representation of the resulting subtradction if the two values are unsigned 8 bit values? (show your work)

6. Convert the following 2-byte binary numbers to hexadecimal, indicating how each part is converted (the binary values are shown with spaces between each 4 digits just to make them easier to read):

1. `0001 0010 0101 1111`

2. `1011 0110 1101 1001`

7. Convert the following hexadecimal numbers to binary, indicating how you converted each digit:

1. `0x23`

2. `0xe1c4`

8. Convert the following decimal values to 8 bit (2’s complement) binary and to hexadecimal. Show your work:

1. `93`

2. `-93`

9. Given the following 4 bit binary values, show the results of each bit-wise operation, showing both the binary and decimal result value for each (list the unsigned decimal value):

1. `0010 | ~(1011)`

2. `0101 & ~(1001)`

3. `0011 ^ 1010`

4. `0111 << 2`

5. `0111 >> 2`

If you enter you answer directly in gradescope, we suggest you first type them into an text file, and then copy and paste into gradescope (just in case you forget to save your answer, you don’t want to lose it). For example, I might create a file in vim into which I’ll enter my answers:

``vim hw2_soln.txt``

• for superscript use `^`, so `2^4` is 2 to the fourth power.

• for subscripts use a number after a variable, so `X3` is X sub 3.

• for hexadecimal values, use the `0x` prefix, so `0x123a` is hexadecimal value 123a.

• for binary values, use the `0b` prefix, so 0b101010 is binary value 101010.

• for decimal values, use no prefix, so 10101 is decimal value 10,101.

For these questions you will be graded on showing how you applied the operation or conversion method we described in class: you must show your work or explain how you got the result to receive credit.

However, you can verify the correctness of your answer to most questions using gdb’s `print` command (or just `p` is shorthand for `print`) with formatting options to print out the value of expressions involving hex, binary, or decimal values in different representations (`p/x` for hex, `p/t` binary, and `p/d` decimal).

``````p/d 0b101010       # print a binary value in decimal
p/d 0xa1f3         # print a hexadecimal value in decimal
p/x 1234           # print a decimal value in hexadecimal (note: gdb does not print leading 0's)
p/t 1234           # print a decimal value in binary (note: gdb does not print leading 0's)
p/t 0x3f           # print a hex value in binary

p/t  0b1010 + 0b0010  # print result of adding two binary in binary
p/d  0b1010 + 0b0010  # print result of adding two binary values in decimal``````

Note: when gdb prints hex and binary values, it does not print out high-order 0 values. For example, printing the addition of these two 8 bit binary values, results in a 5 digit binary value displayed by gdb:

``````p/t 0b00010101 + 0b00000010
\$1 = 10111``````

Your answer should include all 8 digits (or the number of digest specified in the problem. For the example, your answer should be represented like this (and should also include showing your work for how you performed the addition):

``````  00010101
+ 00000010

= 00010111``````

Remember gdb is to help you verify your answers. We want to see how you solved the problem, so show your work in applying the algorithms we discuss in class.

You will submit your solution in gradescope: HW 2: HW 2 submit

In general, for some homework questions you may enter your answer directly in gradescope and for others you may upload an image or .pdf file with your solution in gradescope. Sometimes we give you an option to do either.

There are many different ways to create an electronic file version to upload, some include: