-
-
Notifications
You must be signed in to change notification settings - Fork 50.5k
Expand file tree
/
Copy pathaddition_without_arithmetic.py
More file actions
49 lines (39 loc) · 1.03 KB
/
addition_without_arithmetic.py
File metadata and controls
49 lines (39 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
"""
Illustrate how to add the integer without arithmetic operation
Author: suraj Kumar
Time Complexity: 1
https://en.wikipedia.org/wiki/Bitwise_operation
"""
def add(first: int, second: int) -> int:
"""
Implementation of addition of integer
Examples:
>>> add(3, 5)
8
>>> add(13, 5)
18
>>> add(-7, 2)
-5
>>> add(0, -7)
-7
>>> add(-321, 0)
-321
"""
"""
Add two integers without using arithmetic operators.
This method uses bitwise operations:
- XOR (^) to add bits without carrying
- AND (&) to calculate carry bits
- Left shift (<<) to move the carry to the correct position
"""
while second != 0: # Continue until there is no carry left
c = first & second
first ^= second
second = c << 1
return first
if __name__ == "__main__":
import doctest
doctest.testmod()
first = int(input("Enter the first number: ").strip())
second = int(input("Enter the second number: ").strip())
print(f"{add(first, second) = }")