Here i am implementing a class to do addition operation without using + and - operator.
In this program i am using AND gate (&), XOR gate (^), and a left shift (<<) operator
----------------------------------------------------------------------------------------------------------------------------import java.util.Scanner;
public class TestClass
{
public static void main(String[] args)
{
/*
* object of scanner class to get input from keyboard
*/
Scanner sc = new Scanner(System.in);
System.out.print("enter number1:");
/*
* input int value from keyboard and store it inside variable number1
*/
int number1 = sc.nextInt();
System.out.print("enter number2:");
/*
* input int value from keyboard and store it inside variable number2
*/
int number2 = sc.nextInt();
System.out.print("addition of "+number1+" and "+number2+" is ");
while(number2 != 0)
{
/*
* calculating carry of bits by using logic AND gate operator
*/
int carry = number1 & number2;
/*
* calculating addition by using logic XOR gate operator
*/
number1 = number1 ^ number2;
/*
* left shifting the bits of carry using left shift << operator
*/
number2 = carry << 1;
}
/*
* printing the result
*/
System.out.print(number1);
}
}
No comments:
Post a Comment