0

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;
}
}
2
  • 1
    You never seem to call your methods.You may do things like System.out.println(a + " OR " + b + " is " + or(a,b)); Commented Feb 28, 2017 at 7:41
  • 1
    In your methods you are referring to static variables. This should be changed to local variables, or eliminate the variables completely. For and, you should do only return a&b;. Same for the others, no need to store the result in a temporary. Commented Feb 28, 2017 at 7:42

1 Answer 1

1

You wrote right methods for performing bitwise operator, but seems you are not using them:

You should call methods you have created rather than accessing static variables:

System.out.println(a + " AND " + b + " is " + and(a, b));
System.out.println(a + " OR " + b + " is " + or(a, b));
System.out.println(a + " XOR " + b + " is " + xor(a, b));

Useful link: Bitwise and Bit Shift Operators

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.