CPSC220: Introduction to Computer Architecture (Fall 2013)

Assignment #1

Due by 4:30 pm on Thursday, September 11

Your Job:

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:

Some Advice

Standards:

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

~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 /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.

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