I have this homework assignment where I have to use bit wise operators with methods. I need to use methods for each of the operators. The interface BitOperators are givens that I need to use. I was able to do this without using methods but it is required for me to use methods. Here is what I have but it's not working. I am fairly new to methods so I am not sure what to do.
import java.util.Scanner;
public class TestBitOperators {
public static 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);
}
static int and;
static int or;
static int xor;
public static void main(String[] args) {
byte a;
byte b;
byte l;
byte r;
final byte EXIT = -1;
Scanner stdin = new Scanner(System.in);
do{
System.out.println("Enter a and b numbers in the "
+ "interval [-128,127] (-1 -1 to exit): ");
a = stdin.nextByte();
b = stdin.nextByte();
}
if(a == EXIT && b == EXIT){
break;
}
System.out.println("Enter #left-shift bits in the interval [0,8]: ");
l = stdin.nextByte();
System.out.println("Enter #right-shift bits in the interval [0,8]: ");
r = stdin.nextByte();
}
System.out.println(a + " OR " + b + " is " + and);
System.out.println(a + " OR " + b + " is " + or);
System.out.println(a + " XOR " + b + " is " + xor);
System.out.println(a + " shifted left " + a + " is " + (a << l));
System.out.println(a + " shifted right " + a + " is " + (a >> r));
System.out.println(a + " unsigned-shifted right " + a +
" is " + (a >>> r));
System.out.println(a + " COMPLEMENT " + (~a));
}
while((a < abMAX && b < abMAX) && (a > abMIN && b > abMIN));
}
public static int and(byte a, byte b){
and = a&b;
return and;
}
public static int or(byte a, byte b){
or = a|b;
return or;
}
public static int xor(byte a, byte b){
xor = a^b;
return xor;
}
}
System.out.println(a + " OR " + b + " is " + or(a,b));and, you should do onlyreturn a&b;. Same for the others, no need to store the result in a temporary.