Lfsr
一般斐波那契LFSR的反向輸出
假設我們有一些斐波那契 LFSR,它會輸出一些序列。
如何更改啟動 LFSR,使其輸出完全相同的序列,但順序相反?
其回饋多項式與給定 LFSR 多項式相反的斐波那契 LFSR將產生相反的序列。這裡的反向是指 $ x^mg(x^{-1}) $ 在哪裡 $ g(x) $ (學位的 $ m $ ) 是給定斐波那契 LFSR 的回饋多項式。請注意,LFSR 的初始載入 必須更改為給定輸出序列的結尾(以相反的順序)。
在此處提供 lfsr 和反向 lfsr 的程式碼。第一個是來自 wikipedia 的 lfsr 程式碼,第二個是反向的 lfsr。
#include <stdio.h> int main(void) { unsigned short start_state = 0xace1; unsigned lfsr = start_state; unsigned int period = 0; do { unsigned short lsb = lfsr & 1; lfsr >>= 1; lfsr ^= (-lsb) & 0xb400; ++period; printf("%x\n", lfsr); } while (lfsr != start_state); return 0; }
#include <stdio.h> unsigned short bit_revert(unsigned short v) { int i = 0; unsigned short lsb = 0; unsigned short n = 0; for (i = 0; i < sizeof(unsigned short) * 8; i++) { lsb = v & 1; v >>= 1; n <<= 1; n |= lsb; } return n; } int main(void) { unsigned short start_state = 0xace1; unsigned lfsr = start_state; unsigned int period = 0; do { unsigned short lsb = lfsr & 1; lfsr >>= 1; lfsr ^= (-lsb) & 0x8016; ++period; printf("%x\n", bit_revert(lfsr)); } while (lfsr != start_state); return 0; }