Java program to find the sum of two numbers using binary addition
// Java program to find the sum of two numbers
// using binary addition
import java.util.Scanner;
public class Main {
static int binAddition(int a, int b) {
int c; //carry
while (b != 0) {
c = (a & b) << 1;
a = a ^ b;
b = c;
}
return a;
}
public static void main(String[] args) {
Scanner SN = new Scanner(System.in);
int num1 = 0;
int num2 = 0;
int add = 0;
System.out.printf("Input first integer value: ");
num1 = SN.nextInt();
System.out.printf("Input second integer value: ");
num2 = SN.nextInt();
add = binAddition(num1, num2);
System.out.printf("Binary Addition is: %d\n", add);
}
}
Output
Input first integer value: 34
Input second integer value: 23
Binary Addition is: 57