Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add 2's complement negative support #11

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions alp/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from .core import *
from .tc import *

try:
from .item import *
Expand Down
25 changes: 25 additions & 0 deletions alp/tc.py
Original file line number Diff line number Diff line change
@@ -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

11 changes: 10 additions & 1 deletion convertBinary.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,13 @@
h = alp.Item(**hexDic)

itemsList = [d, o, h]
alp.feedback(itemsList)

# 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)
11 changes: 10 additions & 1 deletion convertDecimal.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,13 @@
h = alp.Item(**hexDic)

itemsList = [b, o, h]
alp.feedback(itemsList)

# 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)
11 changes: 10 additions & 1 deletion convertHex.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,13 @@
o = alp.Item(**octalDic)

itemsList = [d, b, o]
alp.feedback(itemsList)

# 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)
11 changes: 10 additions & 1 deletion convertOctal.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,13 @@
h = alp.Item(**hexDic)

itemsList = [d, b, h]
alp.feedback(itemsList)

# 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)