elias code or elias gamma code is a universal code encoding positive integers it is used most commonly when coding integers whose upperbound cannot be determined beforehand elias code or elias delta code is a universal code encoding the positive integers that includes elias code when calculating both were developed by peter elias calculates the binary number calculates the unary number the compressed data is calculated in two parts the first part is the unary number of 1 log2x the second part is the binary number of x 2log2x for the final result we add these two parts for the first part we put the unary number of x for the first part we put the eliasg of the number calculates the binary number calculates the unary number the compressed data is calculated in two parts the first part is the unary number of 1 log2 x the second part is the binary number of x 2 log2 x for the final result we add these two parts for the first part we put the unary number of x for the first part we put the elias_g of the number
from math import log

log2 = lambda x: log(x, 2)


def binary(x, l=1):
	fmt = '{0:0%db}' % l
	return fmt.format(x)


def unary(x):
	return (x-1)*'1'+'0'

def elias_generic(lencoding, x):
	
	if x == 0:
		return '0'
	
	first_part = 1 + int(log2(x))
	
	a = x - 2**(int(log2(x)))
	
	k = int(log2(x))

	return lencoding(first_part) + binary(a, k)

def elias_gamma(x):
	
	return elias_generic(unary, x)

def elias_delta(x):
	
	return elias_generic(elias_gamma, x)