

新闻资讯
常见问题语法:
Elias Delta Encoding(X)= Elias Gamma encoding (1+floor(log2(X)) + Binary representation of X without MSB.
首先,在为 Elias Delta 编码编写代码之前,我们将实现 Elias delta 编码。
log、floor 函数以执行对数运算。Elias Gamma 中进行编码。floor 和 log 函数,找到 1+floor(log2(X) 并将其存储在变量 N 中。 (N-1)*'0'+'1' 找到 N 的一元编码,它为我们提供了一个二进制字符串,其中最低有效位为 '1',其余最高有效位为 N-1 个'0'。示例: 某些值的 Elias Gamma 编码
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)
MSB。“{0:b}”.format(k) 找到 k 的二进制等效项并将其存储在名为 binary 的变量中。format() 的哪个参数来填充 {}。binary[1:] ,它是 X 的二进制表示,没有 MSB。示例: 不带 MSB 的二进制表示
def Binary_Representation_Without_MSB(x):
binary = "{0:b}".format(int(x))
binary_without_MSB = binary[1:]
return binary_without_MSB
现在我们要为 Elias Delta Encoding 编写代码
Elias Delta 中进行编码。floor 和 log 函数,找到 1+floor(log2(k) 。 1+floor(log2(k) 的结果传递给 Elias Gamma 编码函数。示例:某些值的 Elias Delta 编码
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))
MSB 的 k 的 Elias Gamma 编码和二进制表示的结果为某些整数值生成 Elias Delta 编码的完整代码
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))
输出:
00100110