Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Elias Delta Encoding in Python

  • Difficulty Level : Expert
  • Last Updated : 03 Dec, 2021

In this article, we are going to implement Elias Delta encoding using python.

Syntax:

Elias Delta Encoding(X)= Elias Gamma encoding (1+floor(log2(X))) + Binary representation of X without MSB.   

Implementation

First, we are going to implement Elias delta Encoding, Before writing code for Elias Delta Encoding.

Step 1:

  • Import log, floor functions from the math library to perform logarithmic operations.
  • Fetch input k from user to encode in Elias Gamma.
  • Using floor and log functions in the math module, find 1+floor(log2(X) and store it in variable N.
  • Find the unary coding of N, using (N-1)*’0’+’1′ which gives us a binary string with ‘1’ in a least-significant bit and N-1 ‘0’s in the remaining Most significant bits.

Example: Elias Gamma encoding for some value

Python3




def EliasGammaEncode(k):
    if (k == 0):
        return '0'
    N = 1 + floor(log(k, 2))
    Unary = (N-1)*'0'+'1'
    return Unary + Binary_Representation_Without_MSB(k)

Step 2:

  • Create a function that takes the input X and gives the result as a Binary representation of X without MSB.
  • Using “{0:b}”.format(k) find the binary equivalent of k and store it in a variable named binary.
    • The prefix zero just specifies which argument of format() should be used to fill the {}.
    • b specifies that the argument should be converted to binary form.
  • Return the string binary[1:] which is a Binary representation of X without MSB.

Example: Binary Representation without MSB

Python3




def Binary_Representation_Without_MSB(x):
    binary = "{0:b}".format(int(x))
    binary_without_MSB = binary[1:]
    return binary_without_MSB

Now we are going to write code for Elias Delta Encoding

Step 3:

  • Fetch input k from user to encode in Elias Delta.
  • Using floor and log functions in the math module, find 1+floor(log2(k).
  • Pass the result of 1+floor(log2(k) to Elias Gamma encoding function.

Example: Elias Delta Encoding for some value

Python3




def EliasDeltaEncode(x):
    Gamma = EliasGammaEncode(1 + floor(log(k, 2)))
    binary_without_MSB = Binary_Representation_Without_MSB(k)
    return Gamma+binary_without_MSB
 
 
k = int(input('Enter a number to encode in Elias Delta: '))
print(EliasDeltaEncode(k))

Step 4:

  • Get the result of Elias Gamma encoding and Binary representation of k without MSB
  • Concatenate both the results and print them on the console

Complete code to produce Elias Delta Encoding for some integer value

Python3




from math import log
from math import floor
 
def Binary_Representation_Without_MSB(x):
    binary = "{0:b}".format(int(x))
    binary_without_MSB = binary[1:]
    return binary_without_MSB
 
def EliasGammaEncode(k):
    if (k == 0):
        return '0'
    N = 1 + floor(log(k, 2))
    Unary = (N-1)*'0'+'1'
    return Unary + Binary_Representation_Without_MSB(k)
 
def EliasDeltaEncode(x):
    Gamma = EliasGammaEncode(1 + floor(log(k, 2)))
    binary_without_MSB = Binary_Representation_Without_MSB(k)
    return Gamma+binary_without_MSB
 
k =  14
print(EliasDeltaEncode(k))

Output:

00100110

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!