
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java Program to Add two Numbers without using Arithmetic Operator
Arithmetic operators such as "+", "-", "*", and "/" are used to perform mathematical operations like addition, subtraction, multiplication, modulo, and division.
We have added two numbers using the "+" operator, but in this article, we are going to learn a few Java programs that can add two numbers without using arithmetic operators. The following operators best serve this purpose:
-
Increment operators (++)
-
Bitwise operators
Using Increment Operator
In Java, the increment operators are used to increment the value of a variable by 1. There are two types of increment operators in Java:
-
pre-increment: When we use the "++" operator as a prefix, then it becomes the pre-increment operator. It first increases the value and then performs the operation.
-
post increment: When we use the "++" operator as postfix, it becomes the post-increment operator. But it first uses the variable for operation and then increases its value.
For adding two numbers without using arithmetic operators in a Java program, we can use either of these operators.
Example: pre and post-increment Operators Difference
We will understand the difference between pre-increment and post-increment operators through this example.
import java.util.*; public class Main{ public static void main(String[] args){ int num = 9; int num2 = num++; // post increment System.out.println("Value of num2: " + num2); System.out.println("Value of num after post increment: " + num); int num3 = ++num; // pre increment System.out.println("Value of num3: " + num3); } }
Output:
Value of num2: 9 Value of num after post increment: 10 Value of num3: 11
Example: Adding two Numbers using the Increment Operator
In the program below, we are using the post-increment operator. It will increment the value of n1 till n2 to find the sum of the given numbers.
import java.util.*; public class Main { public static int sum(int n1, int n2) { for(int i=1; i<=n2; i++) { n1++; } return n1; } public static void main(String[] args) { System.out.println("Sum = " + sum(23,5)); } }
On executing, you will get the sum of two numbers:
Sum = 28
Using Bitwise Operators
The bitwise operators first convert the operands into their binary format and then perform the given operation. We use bitwise AND(&), XOR(^), and the Left-Shift operator(<<) to find the sum of two numbers.
Bitwise AND checks if both bits of the operands are 1, then it returns 1; otherwise, 0. Following is the truth table of AND operation -
a |
b |
a&b |
---|---|---|
0 |
0 |
0 |
0 |
1 |
0 |
1 |
0 |
0 |
1 |
1 |
1 |
Bitwise XOR returns 1 if both bits are opposite, otherwise 0. Following isthe truth table of the XOR operation -
a |
b |
a^b |
---|---|---|
0 |
0 |
0 |
0 |
1 |
1 |
1 |
0 |
1 |
1 |
1 |
0 |
Bitwise left-shift operator moves bits of the operand to the left by the specified number of positions. In the expression "x<<n",x will be converted to binary form and then, its bits will be shifted to the number of positions specified by n.
Steps to Add two numbers using Bitwise Operators
Follow the given steps:
-
Step 1: Take a while loop that will run till the second number is not equal to 0.
-
Step 2: Now, perform an AND operation between the given numbers and assign the result to a carry variable in the form of binary.
-
Step 3: Then, perform the XOR operation so that the value of the first number will get updated to the sum of both numbers (in binary form). Since, value of ?n2' will become 0 after the left shift operation, the while loop will stop its execution and 15 will be printed as output.
-
Step 4: Next, use the left shift operation so that the second number will become 0, and the while loop stops its execution.
Example
Let's see the practical implementation of the above steps in a Java program:
import java.util.*; public class Main { public static int sum(int n1, int n2) { while(n2!=0){ int c= n1 & n2; // n1 = 1001, n2 = 0110, c = 0000 n1= n1^n2; // n1 = 1111 after XOR operation n2= c << 1; // n2 = 0000 } return n1; } public static void main(String[] args) { System.out.println("Sum = " + sum(9, 6)); } }
Output:
Sum = 15