Java Program to Add Two numbers Without using Arithmetic Operator Last Updated : 20 Apr, 2023 Summarize Comments Improve Suggest changes Share Like Article Like Report Here, we need to write a function that returns the sum of two stated integers. And the function must not utilize any of the arithmetic operators such as +, ++, –, -, .. Etc.). It will be a very basic elementary level program to compute the sum by simply using the '+' operator and thereby simply printing and displaying out the resultant. But if we refrain from constraint not to use any of arithmetic operators then there is only one way out which is with the use of XOR operator which can perform an addition operation for two given bits. And for the carry bit, we can use AND (&) operator. It is done using the bit-wise operators. Here we are going to use mainly three operations as follows: Bit-wise XORBit-wise ANDBit-wise left shift operator First, the numbers are converted into binary format. Considering 8 indices for an integer data type. Now the carry is handled by a bit-wise left shift operator and the rest of the binary is displayed on the screen as an integer number depicting the sum of the above two integers. It is depicted below: Example: Input : 1, 2Output : 3Explanation: a = 1 : 00000001b = 2 : 00000010----------------c = 3 : 00000011 Implementation: Java // Java Program to find the Sum of Two Numbers // Without Using any Arithmetic Operator // Importing input output classes import java.io.*; // Main class class GFG { // Method to sum two integer elements static int Sum(int a, int b) { // Iterating until there is no carry while (b != 0) { // Using AND operator int carry = a & b; // Using XOR operator a = a ^ b; // Shifting the carry by one so that // adding it to the integer 'a' // gives the desired output b = carry << 1; } return a; } // Method 2 // Main driver method public static void main(String arg[]) { // Print the sum of custom integer numbers to be // summed up passed as an arguments System.out.println(Sum(2, 3)); } } Output5 Time Complexity: O(log2(b))Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Java Program to Add Two Binary Strings K Kanchan_Ray Follow Improve Article Tags : Java Java Programs Practice Tags : Java Similar Reads Java Program to Add Two Numbers Given two integers num1 and num2, the task is to find the sum of the given two numbers in Java. Example of Addition of Two Numbers Input: A = 5, B = 6Output: sum = 11 Input: A = 4, B = 11Â Output: sum = 15 Â Program to Add Two Numbers in Java Below is the implementation of adding two Numbers are ment 4 min read Java Program to Add two Complex Numbers Complex numbers are numbers that consist of two parts â a real number and an imaginary number. Complex numbers are the building blocks of more intricate math, such as algebra. The standard format for complex numbers is a + bi, with the real number first and the imaginary number last. General form fo 3 min read Java Program to Calculate Sum of Two Byte Values Using Type Casting The addition of two-byte values in java is the same as normal integer addition. The byte data type is 8-bit signed two's complement integer. It has a minimum value of -128 and a maximum value of 127 (inclusive). The Sum of two-byte values might cross the given limit of byte, this condition can be ha 2 min read Java Program to Add Two Binary Strings When two binary strings are added, then the sum returned is also a binary string. Example: Input : x = "10", y = "01" Output: "11"Input : x = "110", y = "011" Output: "1001" Explanation: 110 + 011 =1001Approach 1: Here, we need to start adding from the right side and when the sum returned is more th 3 min read Java Program to Illustrate Use of Binary Literals A binary literal is a number represented in binary (0s and 1s). In Java, you can use binary literals for integral types such as byte, short, int, and long. To specify a binary literal, prefix the number with 0b or 0B. This allows you to define numbers in binary format directly in your code. When exe 4 min read Java Program For Arithmetic Operations Between BigDecimal and Primitive Data Types The floating-point data types (float and double) are not as accurate to be used in the financial calculations. Therefore, Java offers a separate class "BigDecimal" to perform the operations and avoid the minimal chances of mistakes in calculations. BigDecimal class provides operations on double numb 6 min read Like