Java Challenge: Can You Explain This Bitwise + Recursive Arithmetic Program?
Neelakandan R

Neelakandan R @neelakandan_ravi

About: I have worked in bank for 8 month and after some gap in my career,now currently started learning full stack java to restart my career...😜

Location:
Chennai
Joined:
Dec 19, 2024

Java Challenge: Can You Explain This Bitwise + Recursive Arithmetic Program?

Publish Date: Aug 5 '25
0 2

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);
    }

}

Enter fullscreen mode Exit fullscreen mode

Output:
add :6
sub-10
multi :35
quotient :12 reminder :4

Comments 2 total

  • Gowtham Sankar Gunasekaran
    Gowtham Sankar Gunasekaran Aug 5, 2025

    Hello neelakandan_ravi,

    All methods avoid +, -, *, / operators explicitly.

    Bitwise operations (&, |, ^, ~, <<) handle binary representations.

    Recursion and loops replace direct arithmetic.

    • Neelakandan R
      Neelakandan RAug 5, 2025

      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.

Add comment