CPSC220: Introduction to Computer Architecture (Fall 2013)

Assignment #1

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

Your Job:

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
   */
  public BinaryInteger add(BinaryInteger 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:

Some Advice

Standards:

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

~lasseter/classes/cpsc220/username

where username should be replaced by your username.

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 ~lasseter/classes/cpsc220/username
    
  2. Use ls to check that it worked - you should see a directory hw1 inside your handin directory, and files BinaryInteger.java and TestBinaryInt.java (or whatever you call it) inside the hw1 directory. It is OK if there are other files such as BinaryInteger.class.

    
          ls ~lasseter/classes/cpsc220/username
          ls ~lasseter/classes/cpsc220/username/hw1