The Wayback Machine - https://web.archive.org/web/20240724223538/https://www.geeksforgeeks.org/elias-gamma-decoding-in-python/
Open In App

Elias Gamma Decoding in Python

Last Updated : 28 Nov, 2021
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report

The Elias gamma code is a universal code that is used to encode a sequence of positive integers. It is developed by Peter Elias. It is most useful when the upper-bound of integers cannot be determined beforehand.

Formula:

Elias Gamma Coding=Unary(1+floor(log2(x)))+Binary representation of ‘x’ without MSB

Example: Let’s consider an example where we want to decode 0001001,

Apply Step 1:
Count the number of '0's from MSB until you reach the first '1' and store the count in K.
In our example(0001001) K=3 

Apply Step 2:
Read 3 more bits including the first '1'=1001

Apply Step 3:
Convert the final binary into integer which gives us the original number.
Decimal(1001)=9

Stepwise Implementation

Step 1: Count the number of ‘0’s from MSB until you reach the first ‘1’ and store the count in K.

Python3




# define the function
def Elias_Gamma_Decoding(x):
    
    # convert to list
    x = list(x)
      
    # initialize k to 0
    K = 0
    while True:
        
        # check if k is not 0 in through
        # list index
        if not x[K] == '0':
            break
          
        # increment k value
        K = K + 1


Step 2: Consider that ‘1’ as the first digit and read ‘K’ more bits from the current ‘1’

Python3




x = x[K:2*K+1]  # Reading K more bits from '1'


Step 3: Convert the final binary into an integer which gives us the original number.

Python3




# Converting binary to integer
for i in range(len(x)):
    if x[i] == '1':
        n = n+math.pow(2, i)
return int(n)


Below is the complete implementation of the above approach.

Python3




# import the math module
import math
  
# function
def Elias_Gamma_Decoding(x):
    x = list(x)
    K = 0
    while True:
        if not x[K] == '0':
            break
        K = K + 1
      
    # Reading K more bits from '1'
    x = x[K:2*K+1]
  
    n = 0
    x.reverse()
      
    # Converting binary to integer
    for i in range(len(x)):
        if x[i] == '1':
            n = n+math.pow(2, i)
    return int(n)
  
  
# value input
x = '0001001'
  
# call the function
print(Elias_Gamma_Decoding(x))


Output:

9


Similar Reads

Elias Gamma Encoding in Python
The Elias gamma code is a universal code that is used to encode a sequence of positive integers. It is developed by Peter Elias. It is most useful when the upper bound of integers cannot be determined beforehand. Steps in Encoding: To encode a number X, Find the largest N, with [Tex]2^{N}\leq X[/Tex](greater power of 2). Encode N using Unary coding
1 min read
Elias Delta Decoding in Python
In this article, we are going to implement Elias Delta Decoding using python. Peter Elias devised the Elias delta code, which is a universal system for encoding positive integers. Syntax: Elias Delta Encoding(X)= Elias Gamma encoding (1+floor(log2(X))) + Binary representation of X without MSB. Approach:Import required libraries and read the encoded
2 min read
Elias Delta Encoding in Python
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
3 min read
Python math.gamma() Method
Python in its language allows various mathematical operations, which has manifolds application in scientific domain. One such offering of Python is the inbuilt gamma() function, which numerically computes the gamma value of the number that is passed in the function. Syntax : math.gamma(x) Parameters : x : The number whose gamma value needs to be co
2 min read
scipy stats.gamma() | Python
scipy.stats.gamma() is an gamma continuous random variable that is defined with a standard format and some shape parameters to complete its specification. Parameters : -> q : lower and upper tail probability -> x : quantiles -> loc : [optional]location parameter. Default = 0 -> scale : [optional]scale parameter. Default = 1 -> size :
2 min read
Python | sympy.gamma() method
With the help of sympy.gamma() method, we can find the gamma function in SymPy. gamma(z) represents [Tex]\Gamma(z) = \int_0^\infty t^{z - 1}e^{-t}\, dt, [/Tex] which for positive integer z is the same as [Tex](z - 1)![/Tex]. Syntax: gamma(Z) Parameters: Z - It is the input value to the gamma function. Returns: Returns the gamma function result of t
1 min read
Python - Log Gamma Distribution in Statistics
scipy.stats.loggamma() is a log gamma continuous random variable. It is inherited from the of generic methods as an instance of the rv_continuous class. It completes the methods with details specific for this particular distribution. Parameters : q : lower and upper tail probability x : quantiles loc : [optional]location parameter. Default = 0 scal
2 min read
Wand gamma() function in Python
gamma() allows us to adjust the luminance of an image. Resulting pixels are defined as pixel^(1/gamma). The value of gamma is typically between 0.8 & 2.3 range, and value of 1.0 will not affect the resulting image. Parameters : Parameter Input Type Description adjustment_value numbers.Real value to adjust gamma level. channel basestring optiona
1 min read
Pgmagick gamma() method - Python
The gamma() function is an inbuilt function in the Pgmagick library which is used to level of gamma correction. The function returns the true value on success. Syntax: gamma(red, green, blue) Parameters: This function accepts three parameters as red, green and blue which correct the gamma level of red, green and blue color respectively. Return Valu
1 min read
numpy.random.gamma() in Python
With the help of numpy.random.gamma() method, we can get the random samples of gamma distribution and return the random samples of numpy array by using this method. Syntax : numpy.random.gamma(shape, scale=1.0, size=None) Return : Return the random samples of numpy array. Example #1 : In this example we can see that by using numpy.random.gamma() me
1 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg