Details of Elias Delta encoding in Python
- 2021-12-13 08:32:04
- OfStack
Directory 1, Step by Step Step 1: Step 2: Step 3: Step 4:
Return string
Syntax:
Elias Delta Encoding(X)= Elias Gamma encoding (1+floor(log2(X)) + Binary representation of X without MSB.
1. Step by step implementation
First of all, in for
Elias Delta
Before writing the code, we will implement the
Elias delta
Coding.
Step 1:
Import from Math Librarylog
,
floor
Function to perform logarithmic operations.
The input k is obtained from the user to
Elias Gamma
Encoding in.
Use the
floor
And
log
Function to find the
1+floor(log2(X)
And store it in the variable N.
Use
(N-1)*'0'+'1'
Find the 1-yuan code of N, which provides us with a binary string, where the least significant bit is' 1 'and the remaining most significant bits are N-1' 0 '.
Example: For some values
Elias Gamma
Code
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 accepts the input X and gives the result as a binary representation of X, withoutElias delta
0
.
Use
“{0:b}”.format(k)
Find the binary equivalent of k and store it in a file named
binary
In the variable of.
The prefix zero only specifies that the
format()
Which parameter to populate {}.
b specifies that parameters should be converted to binary form.
Return string
binary[1:]
Which is a binary representation of X and does not
Elias delta
0
.
Example: Without
MSB
Binary representation of
def Binary_Representation_Without_MSB(x):
binary = "{0:b}".format(int(x))
binary_without_MSB = binary[1:]
return binary_without_MSB
Now we have to work for
Elias Delta Encoding
Write code
Step 3:
Get the input k from the user toElias Delta
Encoding in.
Use the
floor
And
log
Function to find the
1+floor(log2(k)
.
Will
1+floor(log2(k)
The result is passed to the
Elias Gamma
Encoding function.
Example: For some values
Elias Delta
Code
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 withoutMSB
k of
Elias Gamma
Results of encoding and binary representation
Connect the two results and print them on the console
Generate for some integer values
Elias Delta
Complete code of coding
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