汉明码与自我纠错的比特
理查德·汉明在 1950 年提出的编码,通过加入校验位,让接收方能够检测并纠正单个翻转的比特。切换四个数据位,翻转七个编码位中的任意一位,观察校验子如何指出错误位置。
even parity · syndrome = XOR of positions with a 1 = 0 → no error detected
Four data bits are placed at positions 3, 5, 6, and 7. Parity bits at 1, 2, and 4 are computed with even parity. Click any encoded bit to inject a single error; the syndrome is the XOR of the positions holding a 1, which directly names the corrupted position. If two bits flipped at once, the syndrome would point at a third position — that is why Hamming(7,4) only guarantees single-bit correction.
你看到的是什么
四个数据位放在 7 位码字的第 3、5、6、7 位。第 1、2、4 位存放由相互重叠的子集算出的校验位。
每个校验位让对应子集具有偶校验。当某一位翻转时,包含它的子集就会破坏校验。
校验子是把所有为 1 的位置做异或。非零校验子就是出错位置,把它翻回去即可修复码字。
为什么重要
纠错码让数据在内存、存储和通信中保持完整。汉明的想法是内存 ECC 及网络与深空探测器所用编码方案的源头。
复杂度与细节
对 n = 7 位而言,编码与校验子计算都是 O(n)。已知校验子后,纠正一个错误为 O(1)。
常见问题
它能修复两个翻转的比特吗?+
不能。两个比特翻转时,校验子是这两个位置的异或,会指向别处,纠正可能出错。Hamming(7,4) 只保证纠正单个比特,虽然能检测部分双比特错误。
为什么校验位在第 1、2、4 位?+
它们是 2 的幂,二进制表示中只有一位是 1。按这些位是否被置位对位置分组,就能让校验子恰好等于出错位置。