mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-05-22 16:31:15 -04:00
play with bits
This commit is contained in:
parent
c77eb0dd00
commit
4b22537ee7
5 changed files with 73 additions and 0 deletions
17
bit_manipulation/sum_with_bitwise.py
Normal file
17
bit_manipulation/sum_with_bitwise.py
Normal file
|
@ -0,0 +1,17 @@
|
|||
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
|
Loading…
Add table
Add a link
Reference in a new issue