/ Assignment / assignment_24.java
assignment_24.java
 1  /*
 2   * Write code to accept 2 numbers and print the max of the 2 without using comparison operators. 
 3   * Allowed operators are multiplication, division, addition , subtraction, moduluo operators
 4   */ 
 5  
 6  import java.util.Scanner;
 7  
 8  public class assignment_24 {
 9    public static Integer bitMasking(int a , int b) {
10      int diff = a - b; 
11      return ((diff >> 31) & 1); // int is 32 bits, and the 32nd bit is for sign 
12      // if 32nd bit is 0, it is +ve 
13      // if 32nd bit is 1, it is -ve 
14    }
15  
16    public static Integer arithmeticOperators(int a, int b) {
17      int diff = a - b; 
18      return ((a + b) + (diff * ((diff * 2 + 1) % 2)))/2;
19    }
20    public static void main(String[] args) {
21      Scanner sc = new Scanner(System.in);
22      int num1 = sc.nextInt();
23      int num2 = sc.nextInt();
24      sc.close();
25  
26      int bit_mask_op = bitMasking(num1, num2);
27      System.out.println("Bitmasking output");
28      if(bit_mask_op == 0) {
29        System.out.println(num1);
30      }
31      else {
32        System.out.println(num2);
33      }
34  
35      int arithmetic_operator_op = arithmeticOperators(num1, num2);
36      System.out.println("Arithmetic Operator output");
37      System.out.println(arithmetic_operator_op);
38    }
39  }