Skip to content

Commit 02a6ff1

Browse files
author
Ankit Agarwal
committed
Added new file: add_without_operator.py
The file uses bitwise operators to add two numbers instead of the '+' operator.
1 parent 34fd65e commit 02a6ff1

1 file changed

Lines changed: 21 additions & 0 deletions

File tree

‎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)