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

Write a Java application

class TestBitOperatorsthatimplementstheinterface 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

aANDed with parameterb. The method returnsthis.## BitOperators or(byte a, byte b)

This method uses Java's bit-wise OR operator to print the value of parameter

aOR'd with parameterb. The method returnsthis.## BitOperators xor(byte a, byte b)

This method uses Java's bit-wise XOR (Exclusive-OR) operator to print the value of parameter

aXOR'd with parameterb. The method returnsthis.## BitOperators shift(byte n, byte l, byte r)

This method uses Java's bit-wise shift operators on parameter

n. The method printsnshifted leftlnumber of bits followed bynshifted rightrnumber of bits followed bynunsigned shifted rightrnumber of bits. The method returnsthis.## 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 returnsthis.## class TestBitOperators

This program gets four inputs from the users. The first two inputs (

aandb) 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 (landr) are the left- and right- shift width values. Both shift widths must be in the interval [0,8]. Note:ais 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