# CPSC220: Introduction to Computer Architecture (Fall 2013)

## Assignment #1

#### Due at the start of class on Thursday, 09/05/2013 Friday, 09/06/2013

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

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

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

protected BinaryInteger() {}

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

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

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

/**
* Returns the sum of this BinaryInteger and b.
*
* @param  b  a non-null BinaryInteger
* @return      this + b
*/
return null;
}

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

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

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

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

/**
* Returns the value of this BinaryInteger, shifted left by one bit
*/
public BinaryInteger shiftL() { null; }

/**
* Returns the value of this BinaryInteger, shifted right by one bit
*/
public BinaryInteger shiftR() { null; }

}
```

### Constraints

The purpose of this assignment is to build intuition for the way that 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:

• ```
return new BinaryInteger(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.

• 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 BinaryInteger, with all bits inverted
*/
private  BinaryInteger invert() {  ...  }

```

### Standards:

• You must complete two files: BinaryInteger.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 BinaryInteger 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

```
```