整数编码

Is the standard binary representation for positive integers (e.g. cb(5)=101) a uniquely decodeable code ?

【动机】

任一(以终止符结尾)数据文件均可映射成一个(正)整数,因此若有对整数的 prefix code,则其也是对文件的 self-delimiting encoding。文件越大,其对应整数也就越大,难点在于整数范围,同文件大小一样,理论上是无穷的。

How to construct uniquely decodeable code for integers

这里整数编码是指对在二进制编码基础上实现唯一可译,而非指计算机中补码,反码等对数据的存储

对正整数唯一可译编码两种方案:

Unary code

一元码,发送 n-1 个 0 和一个 1
../../PHOTO/整数编码/file-20240820234220669.png|400

Self-delimiting codes

自定界符号码,对二进制编码长度进行编码,并生成一个自定界码

Code Cα
cα(n)=cU[lb(n)]cB(n)

|300

Code Cβ,Cγ,Cδ

当 n 较大时,编码长度近似于原始长度的 2 倍,显然看起来次优有大量冗余的,可以把对长度的编码通过 Cα 缩短,形成一种 “递归”

cβ(n)=cα[lb(n)]cB(n) cγ(n)=cβ[lb(n)]cB(n) cδ(n)=cγ[lb(n)]cB(n)

|250

Codes with end-of-file symbols

Comparing the codes

Elias's 'universal code for integers'

Algorithm Elias's encoder for an integer n.
Write '0'
Loop{
  If logn=0 halt
  Prepend cb(n) to the written string
  n:=logn
}

|700


© 2024 LiQ :) 由 Obsidian&Github 强力驱动