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.
Elias Delta Encoding(X)= Elias Gamma encoding (1+floor(log2(X))) + Binary representation of X without MSB.
- Import required libraries and read the encoded binary string from the user.
- Read/Count the number of zero’s from the most significant bit until you see the first ‘1’ and store it in a variable named ‘L’
L=0 while True: if not x[L] == '0': break L= L + 1
- Consider that ‘L’ as 1st digit and read L more bits and drop all bits until current L bit.
- Take out the remaining bits and prepend ‘1’ in the Most significant bit.
- Convert the final binary into integer which gives us the original number.
Let the input encoded string is 01111
Step1: Read/Count the number of zeros from most significant bit until you see the first ‘1’ and store it in ‘L’ until you see the first ‘1’
In our case, L=1
Step2: Consider that ‘1’ as first digit read L more bits (1 more bit) and drop everything.
Step3: Takeout the remaining bits and prepend with ‘1’ in MSB.
Step4: Convert the final binary string into integer which gives us 7.
Below is the implementation.
Example 1: Example to produce Elias Delta Decoding value corresponding to some value.
Example 2: Example to produce Elias Delta Decoding value corresponding to some value.p