Latest :

Java : Bitwise Operators (AND, OR, XOR) Programs | Java Tutoring

Bitwise operators in Java that may be used to integer types such as long, int, short, char, and byte. The bitwise operator operates on bits and performs bit-by-bit operations.

Bitwise operator in java questions has been a thing since the beginning. Here, we will take care of several such as AND operator java program, XOR bitwise Java program, OR bitwise operator java program, etc.

Suitable examples and sample output will also be given for a proper understanding of the bitwise operators in Java.

This code is for carrying out bitwise operators (such as AND, OR, etc.) using Java language.

  • The problem here is to carry out bitwise operators on two user-defined numbers.
  • Our constraint here is the range of integers that can be used for the operators. This code only supports positive integers from 0 to 65,535 both inclusive.
  • The input is two user-defined int values to be used for bitwise operators.
  • The output is the solution of several bitwise operators carried out on the two input integers in binary format.

Output – 1:

Output -2:

  • Solution :
  1. In the main method, int variables a, b, a1, b1, i are declared. Also, int arrays x, y, AND, OR, XOR are initialized each having a length of 16 in order to store 16 binary digits or bits.
  2. A new Scanner class object is initialized and a reference variable sc is set to represent the object. This class is used to take user input in Java. Scanner class is part of the java.util package and hence the import statement in the beginning is stated to import the functionality of the specified class.
  3. The next line prints a statement to the console instructing the user to enter a number.
  4. The .nextInt() method is called by the Scanner object sc. This method reads an int value and stores it to the variable a.
  5. This same process is repeated again to store the second int variable b.
  6. The method binary is called and a, b, x, y are passed in as the arguments.
  7. In this method, the two parameters a1 and b1 are converted to binary numbers and stored in arrays and y, respectively.
  8. The loop iterates in the reverse direction from index position starting at 15 and ending at 0 in order to start storing binary digits or bits from the right hand side.
  9. For each iteration the integer or the number in decimal form is operated upon by %2 to find a remainder of either 1 or 0 and then divided by 2.
  10. Each iteration helps store one bit from the right hand side in both the arrays. When a1 and b1 are both equal to 0, the conditional statement breaks the implementation of the loop.
  11. example : A number in decimal form 9 is represented as 1001 in decimal form. Assume a1 = 9. Following the iterations, 9%2=1 which gets added to the rightmost position of the binary number.
  12. After that 9/2=4 which is the new value of  a1. 4%2=0 and 4/2=2. Now our binary number has digits 01. Next, 2%2=0 and 2/2=1. Binary number now is 001. Next, 1%2=1 and 1/2=0. We finally get the binary number i.e. 1001 and since a1=0, the loop implementation is broken.
  13. We now have the two input numbers in their binary format with each bit stored in arrays and y, respectively.
  14. After this, in the main method, the methods calAND, calOR, calXOR are implemented with arrays x and y passed in as arguments along with array AND, OR, XOR, for each respective method. All these methods are explained below.
  15. This method simulates the function of a binary AND operator. If both the bits are true (1 represents true and 0 represents false), then the operator returns true. In all other cases, it returns a boolean value of false.
  16. In this method, the loop iterates over all the bits in the array and an AND operation is carried out on each corresponding pair of bits.
  17. The if condition checks if both the bits have value 1 and when this condition being satisfied, the corresponding index position in the AND array is set to 1. It is set to 0 in all other cases.
  18. This method simulates the function of a binary OR operator or binary addition. If any of the bits is true (1 represents true and 0 represents false), then the operator returns true. In all other cases, it returns a boolean value of false.
  19. In this method, the loop iterates over all the bits in the array, and an OR operation is carried out on each corresponding pair of bits.
  20. The int variable is initialized to 0 and is declared and it is set to 0 at the beginning of each iteration.
  21. For each iteration, the two bits are added along with and the sum is stored in s. 
  22. In the next step, s%2 gives us the remainder as 1 or 0 which is stored in the index position in the OR array. Also, the value of s/2 is stored in the variable b. To better understand this, let us see an example.
  23. Example :
    For a certain iteration let us consider the value of two bits to be operated upon as 1 and 1.
    s = 1+1+b = 1+1+0 = 2
    OR[i] = 2%2 = 1
    b = 2/2 = 1
    Here, the addition of 1+1 gives a carry-over of 1 which is stored in b and the answer 0 which is stored in the array.
  24. Thus, the OR bitwise operation or binary addition is carried out in this method.
  25. This method simulates the function of a binary XOR or exclusive OR operator. If both the bits are of the same value (1 represents true and 0 represents false), then the operator returns false. In all other cases, it returns a boolean value of true.
  26. In this method, the loop iterates over all the bits in the array, and an XOR operation is carried out on each corresponding pair of bits.
  27. The if condition checks if both the bits add up to 1 and on this condition being satisfied, the corresponding index position in the XOR array is set to 1. It is set to 0 in all other cases.
  28. Example :
    If both bits are 1, within the conditional statement 1+1=10 or 1+1=0 with a carry-over 1. The condition is not satisfied.
    If one bit is 1 and the other bit is 0, 1+0=1. The condition is satisfied.
  29. Thus, the XOR bitwise operation was successfully implemented using this method.
x

Check Also

Java Program To Find Area Of Rectangle | 3 Ways

Java program to calculate the area of a rectangle. There are maybe so many methods ...