From 5b745be4ab95bf047421d7518db91b644da900d8 Mon Sep 17 00:00:00 2001 From: MrHate Date: Wed, 7 Oct 2020 00:15:37 +0800 Subject: [PATCH] Add 2's complement negative support --- alp/__init__.py | 1 + alp/tc.py | 25 +++++++++++++++++++++++++ convertBinary.py | 11 ++++++++++- convertDecimal.py | 11 ++++++++++- convertHex.py | 11 ++++++++++- convertOctal.py | 11 ++++++++++- 6 files changed, 66 insertions(+), 4 deletions(-) create mode 100644 alp/tc.py diff --git a/alp/__init__.py b/alp/__init__.py index 4b9b127..d83806e 100755 --- a/alp/__init__.py +++ b/alp/__init__.py @@ -1,4 +1,5 @@ from .core import * +from .tc import * try: from .item import * diff --git a/alp/tc.py b/alp/tc.py new file mode 100644 index 0000000..0055752 --- /dev/null +++ b/alp/tc.py @@ -0,0 +1,25 @@ +def decimal2negative(n): + """ + We convert a decimal number into a negative based on the 2's complement. + As an example for an arbitrary decimal larger than 1: + + 0101 (binary for decimal 5) + -> 101 (trim the zeros before the most significant bit) + -> -(2^2)+2^0 (convert binary into decimal under the 2's complment principle) + -> -3 (result) + """ + if (n <= 1): + return 0 + + nn = n + bias = 1 + msb = 0 + n = int(n / 2) + + while (n > 0): + n = int(n / 2) + msb += 1 + bias *= 2 + + return (((1 << msb) - 1) & nn) - bias + diff --git a/convertBinary.py b/convertBinary.py index 8dab6e6..bc3ae3b 100644 --- a/convertBinary.py +++ b/convertBinary.py @@ -24,4 +24,13 @@ h = alp.Item(**hexDic) itemsList = [d, o, h] -alp.feedback(itemsList) \ No newline at end of file + +# calculate negative number +tc = alp.decimal2negative(decimal) +if (tc < 0): + # create associative array and create xml from it + tcDic = dict(title=str(tc), subtitle="2's complement negative after trimming the zeros before the most significant bit", uid="negative", valid=True, arg=str(tc), icon="icons/decimal.png") + t = alp.Item(**tcDic) + itemsList += [t] + +alp.feedback(itemsList) diff --git a/convertDecimal.py b/convertDecimal.py index 282a5f9..3b05c85 100644 --- a/convertDecimal.py +++ b/convertDecimal.py @@ -23,4 +23,13 @@ h = alp.Item(**hexDic) itemsList = [b, o, h] -alp.feedback(itemsList) \ No newline at end of file + +# calculate negative number +tc = alp.decimal2negative(int(sys.argv[1])) +if (tc < 0): + # create associative array and create xml from it + tcDic = dict(title=str(tc), subtitle="2's complement negative after trimming the zeros before the most significant bit", uid="negative", valid=True, arg=str(tc), icon="icons/decimal.png") + t = alp.Item(**tcDic) + itemsList += [t] + +alp.feedback(itemsList) diff --git a/convertHex.py b/convertHex.py index ed420d6..8ab7599 100644 --- a/convertHex.py +++ b/convertHex.py @@ -23,4 +23,13 @@ o = alp.Item(**octalDic) itemsList = [d, b, o] -alp.feedback(itemsList) \ No newline at end of file + +# calculate negative number +tc = alp.decimal2negative(decimal) +if (tc < 0): + # create associative array and create xml from it + tcDic = dict(title=str(tc), subtitle="2's complement negative after trimming the zeros before the most significant bit", uid="negative", valid=True, arg=str(tc), icon="icons/decimal.png") + t = alp.Item(**tcDic) + itemsList += [t] + +alp.feedback(itemsList) diff --git a/convertOctal.py b/convertOctal.py index 0d53ee6..5a3c1c7 100644 --- a/convertOctal.py +++ b/convertOctal.py @@ -24,4 +24,13 @@ h = alp.Item(**hexDic) itemsList = [d, b, h] -alp.feedback(itemsList) \ No newline at end of file + +# calculate negative number +tc = alp.decimal2negative(decimal) +if (tc < 0): + # create associative array and create xml from it + tcDic = dict(title=str(tc), subtitle="2's complement negative after trimming the zeros before the most significant bit", uid="negative", valid=True, arg=str(tc), icon="icons/decimal.png") + t = alp.Item(**tcDic) + itemsList += [t] + +alp.feedback(itemsList)