I308 C307 Data/Information Representation

I308 C307 Homework 2

Due Date: Monday, January 24, 2022.

There is no lab for this homework, but the lectures contain similar examples to those in the exercises.

This homework is made of two parts. In the first part, we will continue the implementation of the Converter class started in Homework 1. The second part contains some written exercises.

Ex. 1. Binary Arithmetic

a. Size in setBinary

In the function setBinary, check if the length of the string passed in is larger than maxSize, and if it is, cap it to maxSize. Make sure only the first maxSize bits are copied into the binary array if the string is larger.

b. Binary Addition Method

Add a method to the class Converter from homework 1 that performs an unsigned addition on binary numbers. The signature of this function should be

void add(Converter a, Converter b)

Here, a and b are the converter objects that you need to add. The attributes to add are just the binary representations. The result should be stored in the target object (this).

The function should start by completing the bits of the smaller (size-wise) of the numbers a and b with 0 up to the size of the larger one. Declare a variable for the carry on and initialize it as 0.

Then you need a loop that goes from 0 to the larger of the sizes of a and b. In each iteration, add the two bits and the carry on variable using the binary arithmetic rules. Store the result in binary[i] and if there is a carry on number, store it in the variable for it. At the end, set binary[largest of the sizes of a and b] as the value of the carry on, provided that this size is not larger than the maxSize-1.

At the end of the loop, if the carry on contains 0, then set the size of the result object (this) as the larger of the sizes of a and b. If it's a 1, then set the size of the result as 1 plus the largest of the two sizes, unless it's larger than maxSize, in which case, cap the size of the result to maxSize.

c. Testing the Method

In the interface, add another option to add two binary numbers. For this purpose, declare two more converter objects and initialize them with new objects at the top of the main. For the addition option, input a binary number using setBinary into each of the two objects, then call the function add on the third one. At the end, output the result. Do not call output directly in the function add, but call it in the main instead.

Ex. 2. Written Part

For this part, you can type it directly as a text or word file, or you can do it on paper and scan it or take a photo of it.

a. Binary Addition

Considering a binary representation on 5 bits, such as in the lecture slides, using two's complement, perform the following binary additions and specify for each of them if there is overflow or not.

7 + 12
-1 -8
13 - 7

-11 + 8

b. Considering the IEEE representation of floating point numbers on 32 bits as discussed in the lecture, convert the following number to base 10 (decimal) notation:

1 01111111 11000000000000000000000

Homework Submission

Upload the files Converter.java and any files needed for Exercise 2, to Canvas, Assignments, Homework 2.