Bit Operations and Asymptotic Analysis

Due Friday, October 10th at 11:59 PM
Recommended completion by October 8th

Table of Contents

Adding with bit operations: Adder.java

Fill in the missing parts (only) of Adder.java without using the operators +, -, /, *, or \%, ++, --, +=, -=, *=, /=, %=, or any method calls. Instead, use the bitwise operators &, |, ^, ~, <<, >>, >>>, and &=, etc. The usual admonition applies: Don't fight the problem!

public class Adder {
  /** The value x+y. */

  public static int add(int x, int y) { 
      /* FILL IN */
      for (i = 0; i < 32; i += 1) {
          /* FILL IN */ 
      }
      /* FILL IN */ 
  }
} 

Implementing a numeric dataype: Nybbles.java

We want a data type that provides an array of integers that are limited in range to $-8$ to $7$. Such integers are representable in 4~bits. Let's suppose we have an application that strains our computer's primary memory capacity and need to fit large arrays of these integers into as little space as possible. Specifically, I'd like to be able to store $N$ integers in an N/8 array (packing 8 4-bit integers into each int). Fill in the template below to provide a suitable small-int array type. Do not perform any new operations in the implementation (you may include as many as you want for testing, if you put them in a different file).

/** Represents an array of integers each in the range -8..7. 
 *  Such integers may be represented in 4 bits (called nybbles). */
public class Nybbles {
    /** An array of size N. */
    public Nybbles(int N) {
        // DON'T CHANGE THIS.
        data = new int[(N+7) / 8];
        this.N = N;
    }

    /** The size of THIS. */
    public int size () { return N; }

    /** Return the Kth integer in THIS array, numbering from 0. 
     *  Assumes 0 <= K < N. */
    public int get(int k) {
        if (k < 0 || k >= N)
            throw new IndexOutOfBoundsException();
        else 
            return /* REPLACE WITH ANSWER */ 0;
    }

    /** Set the Kth integer in THIS array to VAL.  Assumes 
     *  0 <= K < N and -8 <= VAL < 8. */
    public void set (int k, int val) {
        if (k < 0 || k >= N)
            throw new IndexOutOfBoundsException();
        else if (val < -8 || val >= 8)
            throw new IllegalArgumentException();
        else 
            data[ /* REPLACE WITH ANSWER */ 0 ] =
                /* REPLACE WITH ANSWER */ 0;
    }

    // DON'T CHANGE OR ADD TO THESE.
    private private int N;
    private private int[] data;
}

Asymptotic Analysis

For these problems, you should give a brief explanation as part3.txt. You should not use any fancy typesetting tools (like LaTeX, Word, etc.). Just submit a text file. You are not required to explain your solutions, but you are encouraged to do so. Provide simple and tight asymptotic bounds for each of the following. Here, "simple" means roughly "no unnecessary terms or constants" and "tight" means "either the largest $\Omega(\cdot)$ and $O(\cdot)$ bounds you can find, or if possible, a $\Theta(\cdot)$ bound.

  1. $9x^2 + 3x + 14 \log x$
  2. $\log(4x^3 + 2x^2)$
  3. $\sum_{i=0}^{N}\sum_{j=0}^{i} j$
  4. - Hint: Draw an NxN grid and shade in a number of shares equal to this sum
  5. The worst case running time of the following code fragment:
    int j;
    j = 0;
    for (int i = 0; i < N; i += 1) {
       for ( ; j < M; j += 1) {
          if (bump (i, j)) 
             break;
       }
    }
    
    Assume that M and N are integer constants, and bump is a constant-time ($O(1)$) method that returns a boolean result. We're looking for a $\Theta$ result that uses M and N.
  6. The worst case running time of the function f5 for the code below.
    public static int f1 (int n) {
      int x = 0;
      for (int i = 0; i < n; i++) {
          x++;
      }
      return x;
    }
    
    public static int f5 (int n) {
      if (n <= 1) {
        return 1; 
      }
      return f1(n) + f5(n/2) + f5(n/2);
    }
    
    Give your answer as $\Theta$ result that uses n.

Extra Problems: Asymptotic Analysis

These problems will not be graded. Feel free to submit answers, though they won't be examined unless you make a specific request for someone to do so. These problems are intended as particularly challenging problems to reinforce your understanding of asymptotic analysis.

  1. Show that $\log_b f(x) \in \Theta(\log_{10} f(x))$ for any constant $b>1$.
  2. Suppose that $p(x)$ is any polynomial in $x$ with positive coefficients. Show that $\log p(x) \in O(\log x)$.
  3. Bonus problem (no extra points): Suppose that $f(n)$ is a positive, non-decreasing function. Show that $\lceil f(n)\rceil \in O(f(n))$, where $\lceil x \rceil$ is defined as x rounded up to the nearest integer.