master-algorithms-py/bit_operations/sum_with_bitwise.py
bt3gl a85ed914d3 👾
2023-07-30 21:40:09 -07:00

23 lines
405 B
Python

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# author: bt3gl
def get_sum(self, a: int, b: int) -> int:
if a == -b:
return 0
if abs(a) > abs(b):
a, b = b, a
if a < 0:
return - get_sum(-a, -b)
while b:
c = a & b
a, b = a ^ b, c << 1
return a