def delta_decode(binary_string): length, encoded_bits = unary.decode(binary_string) limit_offset = (length + 1) + length # encoding of the value itself, then 'value' number of bits to read first_part, next_part = binary_string[:limit_offset], binary_string[limit_offset:] # we can now extract the first encoded binary bits_to_decode = getvaluefrombinarystring(first_part) - 1 value = (1 << bits_to_decode) + getvaluefrombinarystring(next_part[:bits_to_decode]) consumed_bits = (length + 1) + length + bits_to_decode return value, consumed_bits
def gamma_decode(binary_string): """reads a gamma encoded value from the binary string, return the value and the number of consumed bits""" # assert binary_string[0] == "0" length, encoded_bits = unary.decode(binary_string) offset = length + 1 remaining_bits = binary_string[offset: offset + length] read_value = (1 << length ) + getvaluefrombinarystring(remaining_bits) consumed_bits = length * 2 + 1 return read_value, consumed_bits
def gamma_decode(binary_string): """reads a gamma encoded value from the binary string, return the value and the number of consumed bits""" # assert binary_string[0] == "0" length, encoded_bits = unary.decode(binary_string) offset = length + 1 remaining_bits = binary_string[offset:offset + length] read_value = (1 << length) + getvaluefrombinarystring(remaining_bits) consumed_bits = length * 2 + 1 return read_value, consumed_bits
#Kabopan - Readable Algorithms. Public Domain, 2007-2009 from kbp.univ.unary import encode, decode assert encode(0) == "1" assert encode(1) == "01" assert encode(5) == "000001" assert encode(5, False) == "111110" assert decode("1") == (0, 1) assert decode("000001") == (5, 6)