Skip to content

Commit 9a73f07

Browse files
authored
Refactor addition function documentation and output
Updated docstring to clarify the use of bitwise operations for addition. Modified the output format of the result to include both input values.
1 parent 5793dcb commit 9a73f07

1 file changed

Lines changed: 7 additions & 13 deletions

File tree

‎maths/addition_without_arithmetic.py‎

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@
88

99
def add(first: int, second: int) -> int:
1010
"""
11-
Implementation of addition of integer
11+
Add two integers using bitwise operations instead of arithmetic operators.
12+
- XOR (^) to add bits without carrying
13+
- AND (&) to calculate carry bits
14+
- Left shift (<<) to move the carry to the correct position
1215
1316
Examples:
1417
>>> add(3, 5)
@@ -19,17 +22,8 @@ def add(first: int, second: int) -> int:
1922
-5
2023
>>> add(0, -7)
2124
-7
22-
>>> add(-321, 0)
23-
-321
24-
"""
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
25+
>>> add(-321, 1)
26+
-320
3327
"""
3428

3529
while second != 0: # Continue until there is no carry left
@@ -46,4 +40,4 @@ def add(first: int, second: int) -> int:
4640

4741
first = int(input("Enter the first number: ").strip())
4842
second = int(input("Enter the second number: ").strip())
49-
print(f"{add(first, second) = }")
43+
print(f"{first = }, {second = }, {add(first, second) = }")

0 commit comments

Comments
 (0)