Skip to content

Commit 583c91b

Browse files
authored
Document addition function using bitwise operations
Added docstring explaining the bitwise addition method.
1 parent 2c15b8c commit 583c91b

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

maths/addition_without_arithmetic.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,17 @@ def add(first: int, second: int) -> int:
2222
>>> add(-321, 0)
2323
-321
2424
"""
25-
while second != 0:
25+
26+
"""
27+
Add two integers without using arithmetic operators.
28+
29+
This method uses bitwise operations:
30+
- XOR (^) to add bits without carrying
31+
- AND (&) to calculate carry bits
32+
- Left shift (<<) to move the carry to the correct position
33+
"""
34+
35+
while second != 0: # Continue until there is no carry left
2636
c = first & second
2737
first ^= second
2838
second = c << 1

0 commit comments

Comments
 (0)