try this one - Posted on August Mon 31st 4:51 PM (Never Expires) - Format: python - This is a modified post titled "from line12".
  1. #try this one
  2. def addBinary(a,b):
  3.     length_dif = len(a) - len(b)
  4.     if length_dif < 0:
  5.         a = '0'*(-length_dif) + a
  6.     if length_dif >= 0:
  7.         b = '0'*length_dif + b
  8.     l = len(a)-1
  9.     carry = 0
  10.     res = ''
  11.     while l >=0:
  12.         #s = int(a[l]) + int(b[l]) + carry
  13.         print(carry)
  14.         if (int(a[l]) + int(b[l]) + carry) == 2:
  15.             res = '0' + res
  16.             carry = 1
  17.         if (int(a[l]) + int(b[l]) + carry) == 3:
  18.             res = '1' + res
  19.             carry = 1
  20.         if (int(a[l]) + int(b[l]) + carry) < 2:
  21.             res = str(int(a[l]) + int(b[l]) + carry) + res
  22.             carry = 0
  23.         l -= 1
  24.     if carry:
  25.         return str(carry) + res
  26.     else:
  27.         return res
  28.  
  29. s = addBinary('1010', '1011')
  30. print(s)

New Paste

Paste Options

Recent Pastes

12 days ago

stripe

12 days ago

Untitled

13 days ago

run stripe liste

15 days ago

Untitled

16 days ago

remove duplicate

19 days ago

Untitled

21 days ago

Untitled

21 days ago

Untitled

22 days ago

Untitled

22 days ago

try this one