Skip to content

Instantly share code, notes, and snippets.

@t510599
Last active January 17, 2021 16:20
Show Gist options
  • Save t510599/1d8aae291066ca01c73609c52ab0c07d to your computer and use it in GitHub Desktop.
Save t510599/1d8aae291066ca01c73609c52ab0c07d to your computer and use it in GitHub Desktop.
Binary arithmetic (+, -) with two's complement
import random
from random import randint
print("1-1", "11-11", "11-0", "1-0", sep="\n")
for i in range(100):
op = random.choice(("+", "-"))
if op == "-":
a = randint(2**10, 2**20)
b = randint(0, a)
else:
a, b = randint(0, 2**20), randint(0, 2**20)
if not randint(1, 12) % 3:
print(f"{randint(0, 10) * '0'}{a:b}{op}{randint(0, 10) * '0'}{b:b}")
else:
print(f"{a:b}{op}{b:b}")
print("-1")
import re
rmzero = (lambda s: (lambda p: p if len(p) > 0 else "0")(s.lstrip("0")))
pad = lambda s, l: "0" * (l - len(s)) + s
def twocmp(s):
t, i = str.maketrans("01", "10"), s.rfind("1")
return s[:i].translate(t) + s[i:] if i != -1 else s
def binplus(a, b):
flag, res, a, b = False, "", *[[int(d) for d in x] for x in (a, b)]
for i in range(len(a)-1, -1, -1):
tmp = a[i] + b[i] + 1 if flag else a[i] + b[i]
flag = (tmp >= 2)
res += str(tmp % 2)
return res[::-1] if not flag else (res + "1")[::-1]
def binmath(a, b, op):
a, b = [rmzero(x) for x in (a, b)]
a, b = [pad(x, (l := max(len(a), len(b)))) for x in (a, b)]
if op == "-":
return rmzero((lambda x: x[len(x) - l:])(binplus(a, twocmp(b)))) # overflow
else:
return binplus(a, b)
while (data := input()) != "-1":
n1, op, n2 = re.findall("([01]+)([+-])([01]+)", data)[0]
print(binmath(n1, n2, op))
1-1
11-11
11-0
1-0
10100011111111100001-1110001011101101011
1001001000010010000-101001110000100111
10011001111010110100-10010110000001111101
11001010100111001100-100000101101010
1111100000110110001-1011010010010011010
11101110100101010000+11101000101000101011
000000010100101100011111111+0000001010000111001001001
10011101111101100010-1101001110010011100
110101001011011110-1101011110011000
0011011101100001001001-0000001010000010110100100
11000000110000100100+11100000100111111100
11101010110111010100-10110101000000100111
01001001011100101101-0000000000110101110001010101
0000001000111010011011101+0011111011110101111101
011011010111101100100-000000101011000101000010
1101111100001100001-1000111011001111100
000000011100100010100110011-101111010100011011
00000000001000001101101100001+00011011010101011011101
1101010110011011101+1000010110001010011
10001100111110100-11010110010101
0010001100010101001001+00010000100010110011100
11111101100110010110+101011011110110111
110110010101011000+1100100101001011110
1011010101100011100-1001010111111110010
1110011010110111011+110000000100111101
011011101101110001010+00011001111111111100100
111100101100110101-11100111001011010
00101010011100011100+00000000011000000000000010000
11101100010110000100-11000011110100101001
000000000011100000111001000101+011100101110000001100
110101000101010100-11110010100001001
01100111101110010110-0000000111000001100101000
000000011101100101100100001-0000000010010110101100011100
11110000110010000100+11011100110100011101
11111101101011111000-10010100110101110001
10111011101000001011-1001111100111001110
1001111111010010001+10111110011001110
11111010010000001111-10110111111010011001
00000011011000101110101001+000000001100100010001001011
11010111001111000101+1111100111110011000
1010010000000101010-1000101010011001
11000010001110011010+11110001001011011101
11000111001011011110-10011010001100100001
0000000001001000010101001101-00000101111011111001010
000100010011001011010-0000000011100110111100001
10001000000100111010-1010001010010111111
100001011111001100-10010111100100100
10101000011011010111-10100001000001000001
1111110101101001+11111110011001100
00001000100100101100110+000000011011101101101001
10101100101010011110-101100000011100000
11101011001011001000-10011100000010011011
00011010000101001010-00000000111011101100000
0001110111000011001000+001110110111111100010
000000000010101101011100011010-0011010111001111000
000000000011011111110000110101+0000000001101111111101000100
000000000010000100010011101-0000111011001010110
1100111110001001100+01010101111000100111
100000111010010000-1001111000111000
11111000011011100001+1010010000000011110
000000000101110001000101110+00000010011111101000110110
10001000110111101110-1100000000110001
1001111011001011001+10010001110111100010
00001101001101101001010+0101100111001001111
11101010111110000-1010111110101
11001011001100001010-101011011110100011
111001101101110111+110001110010001000
100110110011000-111011000111
11010110011111001100-1001100100110110101
00000000011110100101111100011+00011000000110110111100
00011101111000101000111+000011110000111000011011
000000011111110010010010010-000000010000011110111110011
10111011100101001101+11010010011110100010
10011111000011010111+10011100101010110001
010001000010101111110+10100101000111001000
0001001010000111101100+0011101101010001100111
10101001011011-10001001101001
11000110100010010110-1010110001100111110
10000000100010111100+1110011100001111001
11111000011010011110-11100001100110100110
10011010111100010-10001000000110
11101110011011001011+11010110001110010101
10101110000110011010+1000000101011100001
1000011101011001100-10001011111000
1110000111110001111+11101000000000111100
1010011101100101000+10010010111010101110
110010000111100111-10001001011010111
1001001000000000101-1001000010000110
0000000001011000011101001110-01000001100000111
11110001011010101011-1100101100111110011
000000001001010110111000011+0000001010110100001010010
10000111100011001010+10101000011100000100
10010001011000100-11010001010111
000011010100100010100101+00100001000001010001
1010000001111010011-1000001010011000101
1100110101001011010-111100111010101001
11001101101010100011-1110100000011101100
00000000010100001010001110001+0011111111100010001011
00000000011011111011101001100-000000000111011101000000000
0000001111100010010010010-0000000001001010100011101111
-1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment