# CPSC220: Introduction to Computer Architecture (Fall 2013)

## Assignment #1

#### Due by 4:30 pm on Thursday, September 11

Construct a class suitable for representing integers in base 2 (binary numbers). Your work should be stored in a class named SignedBinaryInt, according to the following skeleton:

```
public class SignedBinaryInt {
// Internal convention:  least significant digit is at index SZ - 1

private static final int SZ = 16;
private final boolean[] bits = new boolean[SignedBinaryInt.SZ];

protected SignedBinaryInt() {}

public SignedBinaryInt(int x) {   }
public SignedBinaryInt(String s)  {  }

/**
* Returns a 16-character String of the characters '1' and '0',
* representing the bits in this SignedBinaryInt.  The leftmost
* bit is the most significant.  For example, a SignedBinaryInt
* representing the value 42 returns the String "0000000000101010".
*/
public String toString() { return "";  }

/**
* Returns the value of this SignedBinaryInt as an int.
*/
public Integer intValue() { return 42; }

/**
* Returns the sum of this SignedBinaryInt and b.
*
* @param  b  a non-null SignedBinaryInt
* @return      this + b
*/
public SignedBinaryInt add(SignedBinaryInt b) {
return null;
}

/**
* Returns the difference of this SignedBinaryInt and b.
*
* @param  b  a non-null SignedBinaryInt
* @return      this - b
*/
public SignedBinaryInt sub(SignedBinaryInt b) {
return null;
}

/**
* Returns the product of this SignedBinaryInt and b.
*
* @param  b  a non-null SignedBinaryInt
* @return      this * b
*/
public SignedBinaryInt mul(SignedBinaryInt b) {
return null;
}

/**
* Returns the quotient of this SignedBinaryInt and b.
*
* @param  b  a non-null SignedBinaryInt
* @return      this / b
*/
public SignedBinaryInt div(SignedBinaryInt b) {
return null;
}

/**
* Returns the value of this SignedBinaryInt * -1
*
* @return      - this
*/
public SignedBinaryInt negate () {  return null;   }

/**
* Returns the value of this SignedBinaryInt, shifted left by one
* bit.  The sign is preserved, so, for example, "1000000000101010"
* shifted left is "1000000001010100"
*/
public SignedBinaryInt shiftL() { null; }

/**
* Returns the value of this SignedBinaryInt, shifted right by one
* bit.  The sign is preserved, so, for example, "1000000000101010"
* shifted right is "1000000000010101"
*/
public SignedBinaryInt shiftR() { null; }

}
```

## Constraints

The purpose of this assignment is to build intuition for the way that signed integers are represented in the machine, and to that end, there are several constraints on your work:

• The constant value SZ and the array bits are the only fields you can have in your class. In particular, your solution must not store the corresponding int value as an internal field.

This means, for example, that your intValue() method must recompute the result from the contents of bits, every time it is called.

• At no point can your solution make use of the operators defined on int values: +, -, *, and / are off-limits. For example, the following "solution" would receive no credit:

• ```
public SignedBinaryInt add(SignedBinaryInt b) {
return new SignedBinaryInt(this.intValue() + b.intValue());
}

```

(However, it's probably a good idea to implement methods like this as prototypes, while you implement other parts of the class.)

• The number of "bits" is limited by the value of SZ: there are always exactly 16 bits. Note that this means that some computations may result overflow. This is what happens with integer representations in real machines.

• Implement the toString() method first, followed by the constructors and the intValue()method. That way you can test your work as your go.

• As mentioned in the Constraints section, the work you turn in must not make any use of the int operators at all. However, it is a good idea to build quick prototypes of unfinished methods, such as the add() example given above, to facilitate development and testing of the rest of your work.

• You should plan on implementing several "utility" methods (all of which should be private). For example, you'll probably want methods that can compute the "sum" and the "carry" of two bits

```
private static boolean sum(boolean a, boolean b) {  ...  }

private static boolean carry(boolean a, boolean b) {  ...  }

```

Another useful one is negation:

```
/**
*  Returns the value of this SignedBinaryInt, with all bits inverted
*/
private  SignedBinaryInt invert() {  ...  }

```
• Make sure you get the signs right in multiplication and division. The easiest strategy here is to check the signs of the operands before beginning, apply the multiplication/division process to the absolute values of the operands, and negate if necessary.

• As discussed in our text (and as we'll see in class on Monday), division requires two words each for the divisor and the remainder. I'd advise using a pair of double-sized arrays—i.e. 32 "bits" each—

.
• BONUS (not required but kinda cool): Multiplication also requires two "words" to store its result. Can you think of a way to modify the mul() method to do this?

## Standards:

• You must complete two files: SignedBinaryInt.java and a "driver" program, which you use to test the methods in your solution.

Note that I will test your work not only with this file but with my own:

It is essential that you retain all of the naming conventions of publicly-visible constructors and methods in your solution.

• Partial credit will be given for partially correct implementations. However,

• Code that does not compile will receive no credit, for the entire assignment.

• Matters of elegance and style will be weighed. See the Style Guide for details.

## Turn in:

Put the source code files for your SignedBinaryInt class and testing program in a directory called "hw1". Submit the contents of this directory. You do not need to print your files for handin. Your handin directory is

To hand in your files:

1. Copy your entire hw1 directory into your handin directory. Be sure to copy the directory and its files, not just the files.

```
cp -r ~/cpsc220/hw1 /classes/cpsc220/username
```
2. Use ls to check that it worked - you should see a directory hw1 inside your handin directory, and files SignedBinaryInt.java and TestBinaryInt.java (or whatever you call it) inside the hw1 directory. It is OK if there are other files such as SignedBinaryInt.class.

```