Skip to content

Commit

Permalink
Merge pull request keon#35 from ankit167/Add_two_numbers_without_oper…
Browse files Browse the repository at this point in the history
…ator

Add two numbers without using '+' operator
  • Loading branch information
keon authored Apr 26, 2017
2 parents 72d1709 + 02a6ff1 commit 7972926
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions bit/add_without_operator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
"""
The following code adds two numbers without using the '+' operator.
The code uses bitwise operations to add two numbers.
Input: 2 3
Output: 5
"""

def addWithoutOperator(x, y):
while y != 0:
carry = x & y
x = x ^ y
y = carry << 1
print x

def main():
x,y = map(int,raw_input().split())
addWithoutOperator(x,y)

if __name__ == '__main__':
main()

0 comments on commit 7972926

Please sign in to comment.