語(yǔ)法: Elias Delta Encoding(X)= Elias Gamma encoding (1+floor(log2(X)) + Binary representation of X without MSB. 分步實(shí)施首先,在為 Elias Delta 編碼編寫(xiě)代碼之前,,我們將實(shí)現(xiàn) Elias delta 編碼,。 第1步:
示例: 某些值的 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) 第2步:
示例: 不帶 MSB 的二進(jìn)制表示 def Binary_Representation_Without_MSB(x): binary = "{0:b}".format(int(x)) binary_without_MSB = binary[1:] return binary_without_MSB 現(xiàn)在我們要為 Elias Delta Encoding 編寫(xiě)代碼 第 3 步:
示例:某些值的 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)) 第四步:
為某些整數(shù)值生成 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 |
|
來(lái)自: 海擁 > 《待分類(lèi)》