mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-01-19 00:37:02 +00:00
b2e186f4b7
* feat(maths): add function to perform calculation - Add single function to calculate sum of two positive numbers using bitwise operator * docs: add wikipedia url as explanation * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Apply suggestions from code review Co-authored-by: Caeden Perelli-Harris <caedenperelliharris@gmail.com> * Update sum_of_two_positive_numbers_bitwise.py * Update sum_of_two_positive_numbers_bitwise.py * Update sum_of_two_positive_numbers_bitwise.py --------- Co-authored-by: Okza Pradhana <okzapradhana@azko-macbook.local> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Tianyi Zheng <tianyizheng02@gmail.com> Co-authored-by: Caeden Perelli-Harris <caedenperelliharris@gmail.com>
56 lines
1.6 KiB
Python
56 lines
1.6 KiB
Python
"""
|
|
Calculates the sum of two non-negative integers using bitwise operators
|
|
Wikipedia explanation: https://en.wikipedia.org/wiki/Binary_number
|
|
"""
|
|
|
|
|
|
def bitwise_addition_recursive(number: int, other_number: int) -> int:
|
|
"""
|
|
>>> bitwise_addition_recursive(4, 5)
|
|
9
|
|
>>> bitwise_addition_recursive(8, 9)
|
|
17
|
|
>>> bitwise_addition_recursive(0, 4)
|
|
4
|
|
>>> bitwise_addition_recursive(4.5, 9)
|
|
Traceback (most recent call last):
|
|
...
|
|
TypeError: Both arguments MUST be integers!
|
|
>>> bitwise_addition_recursive('4', 9)
|
|
Traceback (most recent call last):
|
|
...
|
|
TypeError: Both arguments MUST be integers!
|
|
>>> bitwise_addition_recursive('4.5', 9)
|
|
Traceback (most recent call last):
|
|
...
|
|
TypeError: Both arguments MUST be integers!
|
|
>>> bitwise_addition_recursive(-1, 9)
|
|
Traceback (most recent call last):
|
|
...
|
|
ValueError: Both arguments MUST be non-negative!
|
|
>>> bitwise_addition_recursive(1, -9)
|
|
Traceback (most recent call last):
|
|
...
|
|
ValueError: Both arguments MUST be non-negative!
|
|
"""
|
|
|
|
if not isinstance(number, int) or not isinstance(other_number, int):
|
|
raise TypeError("Both arguments MUST be integers!")
|
|
|
|
if number < 0 or other_number < 0:
|
|
raise ValueError("Both arguments MUST be non-negative!")
|
|
|
|
bitwise_sum = number ^ other_number
|
|
carry = number & other_number
|
|
|
|
if carry == 0:
|
|
return bitwise_sum
|
|
|
|
return bitwise_addition_recursive(bitwise_sum, carry << 1)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
import doctest
|
|
|
|
doctest.testmod()
|