Assignment: #BitOperators Due: 02/25/2017 Points: 3

Specification

Write a Java application class TestBitOperators that implements the interface BitOperators.

interface BitOperators

This interface consists of methods that use some of Java's bit-wise operators.

   interface BitOperators {
      BitOperators and(byte a, byte b);
      BitOperators or(byte a, byte b);
      BitOperators xor(byte a, byte b);
      BitOperators shift(byte n, byte l, byte r);
      BitOperators comp(byte n);
   }
BitOperators and(byte a, byte b)

This method uses Java's bit-wise AND operator to print the value of parameter a ANDed with parameter b. The method returns this.

BitOperators or(byte a, byte b)

This method uses Java's bit-wise OR operator to print the value of parameter a OR'd with parameter b. The method returns this.

BitOperators xor(byte a, byte b)

This method uses Java's bit-wise XOR (Exclusive-OR) operator to print the value of parameter a XOR'd with parameter b. The method returns this.

BitOperators shift(byte n, byte l, byte r)

This method uses Java's bit-wise shift operators on parameter n. The method prints n shifted left l number of bits followed by n shifted right r number of bits followed by n unsigned shifted right r number of bits. The method returns this.

BitOperators comp(byte n)

This method uses Java's bit-wise COMPLEMENT operator to print the COMPLEMENT of the value of parameter n. The method returns this.

class TestBitOperators

This program gets four inputs from the users. The first two inputs (a and b) are the operands for the bit-wise AND and OR operators and both numbers must be in the interval [-128,127]. The next two inputs (l and r) are the left- and right- shift width values. Both shift widths must be in the interval [0,8]. Note: a is use as the left-side operand the bit-wise shift operators.

Inputs to the program must be stored in variables of type byte. The program loops until the user enters -1 and -1 for the first two inputs. Error messages are printed when invalid inputs are entered.

You are free to use the following code in your code.

public class TestBitOperators implements BitOperators {
 public static void main(String[] argv) {
  ... 
  byte a, b, l, r;  // input variables
  ...
  while (...) {
    ...
    /* 
     * A TestBitOperators object is-a BitOperators object. The
     * interface methods return _this_ allowing for "chaining."
     */
    new TestBitOperators().and(a,b).or(a,b).xor(a,b).shift(a,l,r).comp(a);
    ...
  }
  ...
 }
}
Example Run: java TestBitOperators

The following is how your program needs to interact with the user.

enter a and b numbers in the interval [-128,127] (-1 -1 to exit): 59 18
enter #left-shift bits in the interval [0,8]: 2
enter #right-shift bits in the interval [0,8]: 3
59 AND 18 is 18
59 OR 18 is 59
59 XOR 18 is 41
59 shifted left 2 bits is 236
59 shifted right 3 bits is 7
59 unsigned-shifted right 3 bits is 7
59 COMPLEMENT is -60

enter a and b numbers in the interval [-128,127] (-1 -1 to exit): 8 127
enter #left-shift bits in the interval [0,8]: 0
enter #right-shift bits in the interval [0,8]: 2
8 AND 127 is 8
8 OR 127 is 127
8 XOR 127 is 119
8 shifted left 0 bits is 8
8 shifted right 2 bits is 2
8 unsigned-shifted right 2 bits is 2
8 COMPLEMENT is -9

enter a and b numbers in the interval [-128,127] (-1 -1 to exit): -128 0
enter #left-shift bits in the interval [0,8]: 4
enter #right-shift bits in the interval [0,8]: 3
-128 AND 0 is 0
-128 OR 0 is -128
-128 XOR 0 is -128
-128 shifted left 4 bits is -2048
-128 shifted right 3 bits is -16
-128 unsigned-shifted right 3 bits is 536870896
-128 COMPLEMENT is 127

enter a and b numbers in the interval [-128,127] (-1 -1 to exit): 64 2
enter #left-shift bits in the interval [0,8]: 1
enter #right-shift bits in the interval [0,8]: 1
64 AND 2 is 0
64 OR 2 is 66
64 XOR 2 is 66
64 shifted left 1 bits is 128
64 shifted right 1 bits is 32
64 unsigned-shifted right 1 bits is 32
64 COMPLEMENT is -65

enter a and b numbers in the interval [-128,127] (-1 -1 to exit): -1 -1