Skip to content

Instantly share code, notes, and snippets.

@TimSC
Last active December 15, 2016 23:49
Show Gist options
  • Save TimSC/5f69556e52d626637bfaf41388f00e6d to your computer and use it in GitHub Desktop.
Save TimSC/5f69556e52d626637bfaf41388f00e6d to your computer and use it in GitHub Desktop.
Encode numbers with a negative base (python 2 or 3)
from __future__ import print_function
def EncodeNegBase(n, b): #Converts from decimal
if n == 0:
return "0"
out = []
while n != 0:
n, rem = divmod(n, b)
if rem < 0:
n += 1
rem -= b
out.append(rem)
return "".join(map(str, out[::-1]))
def DecodeNegBase(nstr, b): #Converts to decimal
if nstr == "0":
return 0
total = 0
for i, ch in enumerate(nstr[::-1]):
total += int(ch) * b**i
return total
if __name__=="__main__":
print ("Encode 10 as negabinary (expect 11110)")
result = EncodeNegBase(10, -2)
print (result)
if DecodeNegBase(result, -2) == 10: print ("Converted back to decimal")
else: print ("Error converting back to decimal")
print ("Encode 146 as negaternary (expect 21102)")
result = EncodeNegBase(146, -3)
print (result)
if DecodeNegBase(result, -3) == 146: print ("Converted back to decimal")
else: print ("Error converting back to decimal")
print ("Encode 15 as negadecimal (expect 195)")
result = EncodeNegBase(15, -10)
print (result)
if DecodeNegBase(result, -10) == 15: print ("Converted back to decimal")
else: print ("Error converting back to decimal")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment