Exam 2 study guide

Covering Bits and Recursion – labs 3 and 4

Bits:

The examples below contain a cast to (byte), because the operators return values of type int.

Given an incomplete code snippet using one of the bit operators, complete it to produce a required result. 

              byte bits = 0x44;   

              byte mask = 0x??;    

              byte rslt = (byte) (bits ^ mask);

              //what was the mask if rslt was 0x4b at this point

              //0xF

 

Use a mask to clear (&), set (|), or test (^) bit or a set of bits.

              bits = (byte) 0xC4;  // set bits

              // show a statement to clear all but the most significant bit

              bits = (byte) (bits & 0x80);      // top bit on - use and to clear

What bit pattern (in hex) is left in the byte, bits, after the above operation:

0x80

What is the value of this byte when interpreted as an unsigned decimal number:

128  // unsigned bytes store from 0 to 256

What is the value of this byte when interpreted as a signed decimal number:

-128  // signed bytes store from -128 to +127

Use << to multiply and >> to divide.  Difference between >> and >>>.  Use shift operator in a loop:

              // Show a loop that uses bit operations to successively divide

              // an int by 2 and print whether each divison is odd or even

              // also print whether the original number was odd or even

              int bp = 0xC05907E8;

              for (int i=0; i<32; i++) { // 32 bits in an int

                     System.out.println(

                           String.format("0x%X", bp) + " is " +

                           // String.format("0x%x), ??) formats ?? as a hex number

                           // mask off the top bits and compare with 0

                           ((bp & 1) == 0 ? "even" : "odd"));

                     // unsigned shift bp

                     bp = bp >>> 1;

              }

              // try this and see what it prints then try with >> instead of >>>

 

Recursion:

Activation records:

       public static void main(String[] args) {

              System.out.println(discomfortIndex(100, .85));

       }

      

       private static int discomfortIndex(int temperature, double humidity) {
             
double retVal = temperature * humidity;
             
return (int) retVal;
       }

Assume the return address is type int, and the instruction immediately after the call to discomfortIndex() is at address 1056.

Show the types, variable names and values present in the activation record immediately after the call to discomfortIndex(), but before the body of discomfortIndex() has begun to execute:

int temperature: 100

double humidity: .85

int retVal: 0   // the method hasn’t started yet.  Temp hasn’t been assigned

int valueToReturn: 0

int returnAddr: 1056

 

Show the types, variable names and values present in the activation record immediately after the assignment to temp, but before the return statement has executed:

int temperature: 100

double humidity: .85

int retVal: 85

int valueToReturn: 0   // the return value is written here when the return statement executes

int returnAddr: 1056

 

How many bytes in the activation record:

int temperature: 4

double humidity: 8

int retVal: 4

int valueToReturn: 4

int returnAddr: 4

24 total

 

Recursive design:

Consider SumByRecursion.java in the demos directory:

Modify the basis case, so it does not make a reference to any array element, but the method sum still returns the sum:

Original:

       public static int sum(int[] sa, int startIndex)

       {

              // if on the last element, return the element

              if (startIndex == sa.length-1)

                     return sa[startIndex];

              else {

                     // otherwise return the current element + the sum

                     // of the array elements starting at the next

                     // element

                     return sa[startIndex] + sum(sa, startIndex+1); 

              }

       } 

Modified:

       // let startIndex go clear to sa.length and return 0

       public static int sum(int[] sa, int startIndex)

       {

              // if on the last element, return the element

              if (startIndex == sa.length)

                     return 0;

              else {

                     // otherwise return the current element + the sum

                     // of the array elements starting at the next

                     // element

                     return sa[startIndex] + sum(sa, startIndex+1); 

              }

       }

 

Will the following call to the recursive method cause a stack overflow?

(The following is an image.  You are supposed to solve the problem without executing the code in eclipse.)

Yes.  As it turns out, it does not actually cause a stack overflow, but the write buffer overflows.

The first recursive call passes in 2, the second recursive call passes in 0.  The ‘basis’ case is never hit.