Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 5 additions & 13 deletions maths/addition_without_arithmetic.py
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -23,9 +15,9 @@ 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


Expand All @@ -36,4 +28,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"The sum is: {add(first, second)}")