How To Add Together Ii Integer Numbers Without Using Addition + Or ++ Arithmetics Operator Inwards Coffee - Recursion Example

In this article, nosotros volition receive got a aspect at around other interview inquiry close adding 2 numbers, but without using + or ++ operator. Interview starts alongside a uncomplicated statement, Can yous write a part to add together 2 numbers (integers) without using + or summation arithmetics operator inwards Java? If yous are proficient at maths, it wouldn’t receive got to a greater extent than than a minute to say that, nosotros tin travel subtraction or - operator to add together 2 numbers because a-(-b)== a+b. Well, that’s correct, but existent inquiry starts when interviewer speedily points out that, yous tin non travel whatever arithmetics operator including +,-,*,/++ or --. Programming too Coding questions are an integral part of whatever Java interview. You should ever await a twosome of questions similar this, e.g. swapping 2 numbers without using a temp variable. If yous receive got been giving interviews, thus yous know that it volition eventually come upwardly downs to the bitwise operator inwards Java. 

Yes, nosotros tin add together 2 numbers past times using bitwise too fleck shift operators, which is non arithmetic. The interviewer volition hold out happy past times hearing bitwise operator, but he would similar to come across the code. 

Well, if yous know binary arithmetics or how to add together numbers inwards binary format, yous may hold out familiar alongside fact than the meat of 2 numbers tin hold out obtained past times using XOR functioning too carry, past times using AND operation. This is the fact, yous must retrieve to solve this inquiry or add together 2 integers without using whatever arithmetics operator e.g. plus, minus etc. 

Sometimes interviewer may enquire yous to write both iterative too recursive solution of the same inquiry Since recursion is around other confusing programming technique, it's favored to a greater extent than during interviews. 

In this Java tutorial, nosotros volition come across both recursive too iterative version of our add together method, which calculates meat of 2 numbers without using an arithmetics operator, but using bitshift too bitwise operators inwards Java.



Iterative Solution

 nosotros volition receive got a aspect at around other interview inquiry close adding 2 numbers How to Add Two Integer Numbers without using Plus + or ++ Arithmetic Operator inwards Java - Recursion exampleIterative solution uses whatever sort of loop, e.g. for, spell or do-while. As I said, meat of 2 integer tin hold out obtained past times XOR bitwise operator too acquit tin hold out obtained but AND bitwise operator. We likewise postulate to travel signed left shift Java operator to add together acquit into sum. Here is code for iterative method to add together 2 integers without using summation or minus operator :

 public static int addIterative(int a, int b){  
        while (b != 0){
            int acquit = (a & b) ; //CARRY is AND of 2 bits
          
            a = a ^b; //SUM of 2 bits is H5N1 XOR B
          
            b = carry << 1; //shifts acquit to 1 fleck to calculate sum
        }
        return a;
 }

Code is self explanatory, nosotros are calculating acquit too keeping it inwards a divide variable, than nosotros are storing meat of 2 numbers into variable a, too shifts acquit to 1 fleck past times using signed left shift operator, In guild to add together into sum.

Recursive Solution

Following method uses recursion programming technique to calculate meat of 2 numbers without arithmetics operator e.g. +, - etc. It's recursive version of our before iterative solution. Just similar inwards loop, nosotros are testing for b=0, hither likewise nosotros are doing same thing. We receive got only combined & bitwise too << signed left shift operator calculating too shifting carry.

public static int add(int a, int b){
        if(b == 0) return a;
        int meat = a ^ b; //SUM of 2 integer is H5N1 XOR B
        int acquit = (a & b) << 1;  //CARRY of 2 integer is H5N1 AND B
        return add(sum, carry);
 }

How to add together 2 integers without arithmetics operator Java Example

Here is consummate code example, which includes both iterative too recursive method along alongside a uncomplicated JUnit test. I receive got tested both add together methods for twosome of border cases e.g. adding negative numbers, Integer.MAX_VALUE, adding nothing etc.

/**
 * Java computer program to calculate meat of 2 publish without using add-on or subtraction
 * operator inwards Java. This solution, travel bitwise too bitshift operator instead of maths operator.
 * @author Javin Paul
 */
public course of teaching AddTwoNumbersJava {
  
    public static void main(String args[]) {
      
       System.out.println(" Sum of 110 add together 200 is : " + add(110, 200));
       System.out.println(" Sum of 0 too 0 is : " + add(0, 0));
       System.out.println(" Sum of -10 too +10 is : " + add(-10, 10));
       System.out.println(" Sum of -10 + 200 is : " + add(-10, 200));
       System.out.println(" Sum of 0 + 200 is : " + add(0, 200));
     
    }  
  
    /*
     * Adding 2 publish without using + or summation arithmetics operator using
     * recursion inwards Java. This method uses XOR too AND bitwise operator to
     * calculate meat of 2 numbers;
     */
    public static int add(int a, int b){
        if(b == 0) return a;
        int meat = a ^ b; //SUM of 2 integer is H5N1 XOR B
        int acquit = (a & b) << 1;  //CARRY of 2 integer is H5N1 AND B
        return add(sum, carry);
    }
 
    /*
     * Adding 2 integers without whatever arithmetics operator too using recursion.
     * This solution likewise uses XOR too AND bitwise too << left shift bitshift
     * operator
     */
    public static int addIterative(int a, int b){ 
        while (b != 0){
            int acquit = (a & b) ; //CARRY is AND of 2 bits
          
            a = a ^b; //SUM of 2 bits is H5N1 XOR B
          
            b = carry << 1; //shifts acquit to 1 fleck to calculate sum
        }
        return a;
    }
 
}

Output:
Sum of 110 add together 200 is : 310
Sum of 0 too 0 is : 0
Sum of -10 too +10 is : 0
Sum of -10 + 200 is : 190
Sum of 0 + 200 is : 200

And hither is JUnit assay instance to assay add() too addIterative() method, If yous honor carefully, I receive got used static import characteristic of Java 1.5, to import diverse assert methods similar assertEquals(expected, actual). By the way, I only realized that I made an error here, which won't touching on the trial but it's a mistake. If yous tin request it out thus allow me know :

import org.junit.Test;
import static org.junit.Assert.*;
import static test.AddTwoNumbersJava.*;

/**
 * JUnit tests for add-on without using maths operator inwards Java.
 * @author
 */
public course of teaching AddTwoNumbersJavaTest {  

    /**
     * Test of add together method, of course of teaching AddTwoNumbersJava.
     */
    @Test
    public void testAdd() {
        assertEquals(add(0, 0), (0 + 0));
        assertEquals(add(100, 210), (100 + 210));
        assertEquals(add(-10, 10), (-10 + 10));
        assertEquals(add(0, 200), (0 + 200));
        assertEquals(add(10, 0), (10 + 0));
        assertEquals(add(Integer.MAX_VALUE, 10), (Integer.MAX_VALUE + 10));
    }

    /**
     * Test of addIterative method, of course of teaching AddTwoNumbersJava.
     */
    @Test
    public void testAddIterative() {
        assertEquals(addIterative(0, 0), (0 + 0));
        assertEquals(addIterative(100, 210), (100 + 210));
        assertEquals(addIterative(-10, 10), (-10 + 10));
        assertEquals(addIterative(0, 200), (0 + 200));
        assertEquals(addIterative(10, 0), (10 + 0));
        assertEquals(addIterative(Integer.MAX_VALUE, 10), (Integer.MAX_VALUE + 10));
    }
}

Further Learning
The Coding Interview Bootcamp: Algorithms + Data Structures
Data Structures too Algorithms: Deep Dive Using Java
Algorithms too Data Structures - Part 1 too 2

That's all on How to add together 2 numbers without using summation + arithmetics operator inwards Java. As I said, yous postulate to retrieve that meat of 2 integers inwards binary is equal to XOR of 2 numbers too acquit is equal to AND functioning of 2 numbers. By using bitwise too bitshift operator inwards Java, yous tin easily calculate meat of 2 numbers without whatever arithmetics operator.

Further Reading on Bit Twiddling
Bitwise too bitshift operator are quite extensively used inwards programming interviews, too many problems similar to a higher house tin hold out solved past times in that place usages. Hackers please is i of the greatest mass on writing code using fleck twiddling too fleck manipulation too inwards fact Java library uses many fleck twiddling techniques from this book.

The Art of Computer Programming past times Donald E. Knuth
Cracking the Coding Interview: 150 Programming Questions too Solutions
Hacker's Delight (2nd Edition) By Henry S. Warren


0 Response to "How To Add Together Ii Integer Numbers Without Using Addition + Or ++ Arithmetics Operator Inwards Coffee - Recursion Example"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel