diff --git a/maths/addition_without_arithmetic.py b/maths/addition_without_arithmetic.py index 409604e4c08a..299c526925d8 100644 --- a/maths/addition_without_arithmetic.py +++ b/maths/addition_without_arithmetic.py @@ -8,7 +8,10 @@ def add(first: int, second: int) -> int: """ - Implementation of addition of integer + Add two integers using bitwise operations instead of arithmetic operators. + - XOR (^) to add bits without carrying + - AND (&) to calculate carry bits + - Left shift (<<) to move the carry to the correct position Examples: >>> add(3, 5) @@ -19,10 +22,11 @@ def add(first: int, second: int) -> int: -5 >>> add(0, -7) -7 - >>> add(-321, 0) - -321 + >>> add(-321, 1) + -320 """ - while second != 0: + + while second != 0: # Continue until there is no carry left c = first & second first ^= second second = c << 1 @@ -36,4 +40,4 @@ def add(first: int, second: int) -> int: first = int(input("Enter the first number: ").strip()) second = int(input("Enter the second number: ").strip()) - print(f"{add(first, second) = }") + print(f"{first = }, {second = }, {add(first, second) = }")