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

In this article, nosotros volition receive got a expression at closed to other interview query virtually adding 2 numbers, but without using + or ++ operator. Interview starts alongside a unproblematic statement, Can you lot write a business office to add together 2 numbers (integers) without using + or summation arithmetics operator inwards Java? If you lot are proficient at maths, it wouldn’t receive got to a greater extent than than a minute to say that, nosotros tin role subtraction or - operator to add together 2 numbers because a-(-b)== a+b. Well, that’s correct, but existent query starts when interviewer chop-chop points out that, you lot tin non role whatsoever arithmetics operator including +,-,*,/++ or --. Programming as well as Coding questions are an integral part of whatsoever Java interview. You should ever await a twosome of questions similar this, e.g. swapping 2 numbers without using a temp variable. If you lot receive got been giving interviews, as well as thence you lot know that it volition eventually come upwards downs to the bitwise operator inwards Java. 

Yes, nosotros tin add together 2 numbers past times using bitwise as well as chip shift operators, which is non arithmetic. The interviewer volition last happy past times hearing bitwise operator, but he would similar to run across the code. 

Well, if you lot know binary arithmetics or how to add together numbers inwards binary format, you lot may last familiar alongside fact than the centre of 2 numbers tin last obtained past times using XOR functioning as well as carry, past times using AND operation. This is the fact, you lot must think to solve this query or add together 2 integers without using whatsoever arithmetics operator e.g. plus, minus etc. 

Sometimes interviewer may inquire you lot to write both iterative as well as recursive solution of the same query Since recursion is closed to other confusing programming technique, it's favored to a greater extent than during interviews. 

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



Iterative Solution

 nosotros volition receive got a expression at closed to other interview query virtually 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, piece or do-while. As I said, centre of 2 integer tin last obtained past times XOR bitwise operator as well as behave tin last obtained but AND bitwise operator. We likewise bespeak to role signed left shift Java operator to add together behave 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 behave = (a & b) ; //CARRY is AND of 2 bits
          
            a = a ^b; //SUM of 2 bits is H5N1 XOR B
          
            b = carry << 1; //shifts behave to 1 chip to calculate sum
        }
        return a;
 }

Code is self explanatory, nosotros are calculating behave as well as keeping it inwards a divide variable, than nosotros are storing centre of 2 numbers into variable a, as well as shifts behave to 1 chip past times using signed left shift operator, In lodge to add together into sum.

Recursive Solution

Following method uses recursion programming technique to calculate centre 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 as well as << signed left shift operator calculating as well as shifting carry.

public static int add(int a, int b){
        if(b == 0) return a;
        int centre = a ^ b; //SUM of 2 integer is H5N1 XOR B
        int behave = (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 as well as recursive method along alongside a unproblematic JUnit test. I receive got tested both add together methods for twosome of border cases e.g. adding negative numbers, Integer.MAX_VALUE, adding nada etc.

/**
 * Java programme to calculate centre of 2 lay out without using add-on or subtraction
 * operator inwards Java. This solution, role bitwise as well as bitshift operator instead of maths operator.
 * @author Javin Paul
 */
public cast 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 as well as 0 is : " + add(0, 0));
       System.out.println(" Sum of -10 as well as +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 lay out without using + or summation arithmetics operator using
     * recursion inwards Java. This method uses XOR as well as AND bitwise operator to
     * calculate centre of 2 numbers;
     */
    public static int add(int a, int b){
        if(b == 0) return a;
        int centre = a ^ b; //SUM of 2 integer is H5N1 XOR B
        int behave = (a & b) << 1;  //CARRY of 2 integer is H5N1 AND B
        return add(sum, carry);
    }
 
    /*
     * Adding 2 integers without whatsoever arithmetics operator as well as using recursion.
     * This solution likewise uses XOR as well as AND bitwise as well as << left shift bitshift
     * operator
     */
    public static int addIterative(int a, int b){ 
        while (b != 0){
            int behave = (a & b) ; //CARRY is AND of 2 bits
          
            a = a ^b; //SUM of 2 bits is H5N1 XOR B
          
            b = carry << 1; //shifts behave to 1 chip to calculate sum
        }
        return a;
    }
 
}

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

And hither is JUnit attempt out illustration to attempt out add() as well as addIterative() method, If you lot abide by 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 impact the lawsuit but it's a mistake. If you lot tin signal it out as well as thence 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 cast AddTwoNumbersJavaTest {  

    /**
     * Test of add together method, of cast 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 cast 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 as well as Algorithms: Deep Dive Using Java
Algorithms as well as Data Structures - Part 1 as well as 2

That's all on How to add together 2 numbers without using summation + arithmetics operator inwards Java. As I said, you lot bespeak to think that centre of 2 integers inwards binary is equal to XOR of 2 numbers as well as behave is equal to AND functioning of 2 numbers. By using bitwise as well as bitshift operator inwards Java, you lot tin easily calculate centre of 2 numbers without whatsoever arithmetics operator.

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

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


Belum ada Komentar untuk "How To Add Together 2 Integer Numbers Without Using Addition + Or ++ Arithmetics Operator Inwards Coffee - Recursion Example"

Posting Komentar

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel