Skip to content

Commit 7972926

Browse files
authored
Merge pull request keon#35 from ankit167/Add_two_numbers_without_operator
Add two numbers without using '+' operator
2 parents 72d1709 + 02a6ff1 commit 7972926

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

bit/add_without_operator.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
"""
2+
The following code adds two numbers without using the '+' operator.
3+
The code uses bitwise operations to add two numbers.
4+
5+
Input: 2 3
6+
Output: 5
7+
"""
8+
9+
def addWithoutOperator(x, y):
10+
while y != 0:
11+
carry = x & y
12+
x = x ^ y
13+
y = carry << 1
14+
print x
15+
16+
def main():
17+
x,y = map(int,raw_input().split())
18+
addWithoutOperator(x,y)
19+
20+
if __name__ == '__main__':
21+
main()

0 commit comments

Comments
 (0)