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

In this article, nosotros volition accept a aspect at merely about other interview enquiry most adding 2 numbers, but without using + or ++ operator. Interview starts amongst a uncomplicated statement, Can you lot write a purpose to add together 2 numbers (integers) without using + or summation arithmetics operator inwards Java? If you lot are skillful at maths, it wouldn’t accept to a greater extent than than a minute to say that, nosotros tin post away utilisation subtraction or - operator to add together 2 numbers because a-(-b)== a+b. Well, that’s correct, but existent enquiry starts when interviewer rapidly points out that, you lot tin post away non utilisation whatsoever arithmetics operator including +,-,*,/++ or --. Programming in addition to Coding questions are an integral component subdivision of whatsoever Java interview. You should ever aspect a pair of questions similar this, e.g. swapping 2 numbers without using a temp variable. If you lot receive got been giving interviews, thus you lot know that it volition eventually come upward downs to the bitwise operator inwards Java. 

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

Well, if you lot know binary arithmetics or how to add together numbers inwards binary format, you lot may move familiar amongst fact than the core of 2 numbers tin post away move obtained past times using XOR performance in addition to carry, past times using AND operation. This is the fact, you lot must recollect to solve this enquiry or add together 2 integers without using whatsoever arithmetics operator e.g. plus, minus etc. 

Sometimes interviewer may enquire you lot to write both iterative in addition to recursive solution of the same enquiry Since recursion is merely about other confusing programming technique, it's favored to a greater extent than during interviews. 

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



Iterative Solution

 nosotros volition accept a aspect at merely about other interview enquiry most adding 2 numbers How to Add Two Integer Numbers without using Plus + or ++ Arithmetic Operator inwards Java - Recursion exampleIterative solution uses whatsoever sort of loop, e.g. for, land or do-while. As I said, core of 2 integer tin post away move obtained past times XOR bitwise operator in addition to send tin post away move obtained but AND bitwise operator. We besides necessitate to utilisation signed left shift Java operator to add together send 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 send = (a & b) ; //CARRY is AND of 2 bits
          
            a = a ^b; //SUM of 2 bits is Influenza A virus subtype H5N1 XOR B
          
            b = carry << 1; //shifts send to 1 fleck to calculate sum
        }
        return a;
 }

Code is self explanatory, nosotros are calculating send in addition to keeping it inwards a dissever variable, than nosotros are storing core of 2 numbers into variable a, in addition to shifts send to 1 fleck past times using signed left shift operator, In fellowship to add together into sum.

Recursive Solution

Following method uses recursion programming technique to calculate core 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 besides nosotros are doing same thing. We receive got merely combined & bitwise in addition to << signed left shift operator calculating in addition to shifting carry.

public static int add(int a, int b){
        if(b == 0) return a;
        int core = a ^ b; //SUM of 2 integer is Influenza A virus subtype H5N1 XOR B
        int send = (a & b) << 1;  //CARRY of 2 integer is Influenza A virus subtype 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 in addition to recursive method along amongst a uncomplicated JUnit test. I receive got tested both add together methods for pair of border cases e.g. adding negative numbers, Integer.MAX_VALUE, adding nada etc.

/**
 * Java computer program to calculate core of 2 let on without using add-on or subtraction
 * operator inwards Java. This solution, utilisation bitwise in addition to 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 in addition to 0 is : " + add(0, 0));
       System.out.println(" Sum of -10 in addition to +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 let on without using + or summation arithmetics operator using
     * recursion inwards Java. This method uses XOR in addition to AND bitwise operator to
     * calculate core of 2 numbers;
     */
    public static int add(int a, int b){
        if(b == 0) return a;
        int core = a ^ b; //SUM of 2 integer is Influenza A virus subtype H5N1 XOR B
        int send = (a & b) << 1;  //CARRY of 2 integer is Influenza A virus subtype H5N1 AND B
        return add(sum, carry);
    }
 
    /*
     * Adding 2 integers without whatsoever arithmetics operator in addition to using recursion.
     * This solution besides uses XOR in addition to AND bitwise in addition to << left shift bitshift
     * operator
     */
    public static int addIterative(int a, int b){ 
        while (b != 0){
            int send = (a & b) ; //CARRY is AND of 2 bits
          
            a = a ^b; //SUM of 2 bits is Influenza A virus subtype H5N1 XOR B
          
            b = carry << 1; //shifts send to 1 fleck to calculate sum
        }
        return a;
    }
 
}

Output:
Sum of 110 add together 200 is : 310
Sum of 0 in addition to 0 is : 0
Sum of -10 in addition to +10 is : 0
Sum of -10 + 200 is : 190
Sum of 0 + 200 is : 200

And hither is JUnit essay out instance to essay out add() in addition to addIterative() method, If you lot discover 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 merely realized that I made an error here, which won't acquit on the effect but it's a mistake. If you lot tin post away betoken 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 in addition to Algorithms: Deep Dive Using Java
Algorithms in addition to Data Structures - Part 1 in addition to 2

That's all on How to add together 2 numbers without using summation + arithmetics operator inwards Java. As I said, you lot necessitate to recollect that core of 2 integers inwards binary is equal to XOR of 2 numbers in addition to send is equal to AND performance of 2 numbers. By using bitwise in addition to bitshift operator inwards Java, you lot tin post away easily calculate core of 2 numbers without whatsoever arithmetics operator.

Further Reading on Bit Twiddling
Bitwise in addition to bitshift operator are quite extensively used inwards programming interviews, in addition to many problems similar higher upward tin post away move solved past times at that topographic point usages. Hackers please is i of the greatest mass on writing code using fleck twiddling in addition to fleck manipulation in addition to 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 in addition to Solutions
Hacker's Delight (2nd Edition) By Henry S. Warren



Sumber https://javarevisited.blogspot.com/

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

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel