Given the following Java class containing four custom methods — findsum(), findsub(), multi(), and divide() — can you explain how each method works internally without using standard arithmetic operators?
package Afternoon;
public class oper {
public static void main(String[] args) {
findsum(4, 2);
findsub(10, 20);
System.out.println("multi :"+multi(5, 7));
divide(100, 8);
}
private static void divide(int a, int b) {
int bal = 0;
while (a >= b) {
a = a - b;
bal++;
}
System.out.println("quotient :" + bal + " " + "reminder :" + a);
}
private static int multi(int i, int j) {
if (j == 0) {
return 0;
}
return i + multi(i, j - 1);
}
private static void findsub(int i, int j) {
int total = i + (~j + 1);
System.out.println("sub"+total);
}
private static void findsum(int a, int b) {
while (b != 0) {
int carry = a & b;// 0100 & 0010=0000
a = a ^ b;// 0100 & 0010=0110
b = carry << 1;// 0000
}
System.out.println("add :"+a);
}
}
Output:
add :6
sub-10
multi :35
quotient :12 reminder :4
Top comments (2)
Hello neelakandan_ravi,
All methods avoid +, -, *, / operators explicitly.
Bitwise operations (&, |, ^, ~, <<) handle binary representations.
Recursion and loops replace direct arithmetic.
Thank you! Yes, that's exactly the concept I explored. It’s fun how we can achieve arithmetic without using traditional operators. Appreciate your comment.