diff --git a/maths/addition_without_arithmetic.py b/maths/addition_without_arithmetic.py index 409604e4c..4d76a695e 100644 --- a/maths/addition_without_arithmetic.py +++ b/maths/addition_without_arithmetic.py @@ -1,14 +1,6 @@ -""" -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 + Add two integers using bitwise operations. Examples: >>> add(3, 5) @@ -23,12 +15,11 @@ def add(first: int, second: int) -> int: -321 """ while second != 0: - c = first & second - first ^= second - second = c << 1 + carry = first & second # Calculate carry + first = first ^ second # Add without carry + second = carry << 1 # Prepare carry for next iteration return first - if __name__ == "__main__": import doctest @@ -36,4 +27,4 @@ if __name__ == "__main__": first = int(input("Enter the first number: ").strip()) second = int(input("Enter the second number: ").strip()) - print(f"{add(first, second) = }") + print(f"The sum is: {add(first, second)}")