Skip to content

Sum of two numbers without + or - operators #3435

@samratpodder

Description

@samratpodder

Is your feature request related to a problem? Please describe.
It is not related to a problem. I want to add an algorithm to add two integers without using addition or subtraction operators. It can be done with Bit Manipulation.

Describe the solution you'd like
Add the 0+1 and 1+0 with XOR and to handle carry we perform a bitwise AND of two 1+1, left shift bit by one place then continue the addition of carry to the sum.

Describe alternatives you've considered
We can also use ++ and -- operators in a loop.

Additional context
None

Test Cases
Input: a=1 b= 2
Output: 3

Input: a=2 b= 3
Output: 5

I want to contribute this to the Math section of theAlgorithms. @siriak @yanglbme

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions