- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing

- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM

- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers

- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources

# Finding the sum of two numbers without using '+', '-', '/', '*' in JavaScript

We are required to write a JavaScript function add() that takes in two numbers m and n. The function should, without using the four basic arithmetic operations add the two numbers taken as input and return the sum.

## Example

The code for this will be −

const m = 67, n = 33; const add = (x, y) => { while(y !== 0){ let carry = x & y; x = x ^ y; y = carry << 1; }; return x; }; console.log(add(m, n));

## Explanation

Sum of two bits can be obtained by performing XOR (^) of the two bits. And the carry bit can be obtained by performing AND (&) of two bits.

We have here used an extended version of the Half Adder logic that can be used to add 2 single bits to add two integers. If x and y don’t have set bits at the same position(s), then bitwise XOR (^) of x and y gives the sum of x and y.

To incorporate common set bits also, bitwise AND (&) is used. Bitwise AND of x and y gives all carry bits. We have calculated (x & y) << 1 and added it to x ^ y to get the required result.

## Output

And the output in the console will be −

100

- Related Articles
- Finding two numbers given their sum and Highest Common Factor using JavaScript
- Finding lunar sum of Numbers - JavaScript
- Finding two numbers that produce equal to the sum of rest in JavaScript
- Subtracting two numbers without using the (-) sign JavaScript
- Find the Sum of two Binary Numbers without using a method in C#?
- Finding LCM of more than two (or array) numbers without using GCD in C++
- Finding sum of remaining numbers to reach target average using JavaScript
- Finding two golden numbers in JavaScript
- Finding the sum of all numbers in the nth row of an increasing triangle using JavaScript
- C Program to find sum of two numbers without using any operator
- Finding sum of all numbers within a range in JavaScript
- Finding pandigital numbers using JavaScript
- Finding square root of a number without using Math.sqrt() in JavaScript
- Finding closest pair sum of numbers to a given number in JavaScript
- Finding sum of multiples in JavaScript