长期程序员 - 完全新手。这是代码。这是我第一次尝试发电机。我正在尝试生成一个 lfsr 序列。本质上,每次你都向右移动一个。如果您只是移出1一点,则与该tap值异或。package mainimport ( "fmt" "math/big")// lfsr returns an lfsr generator.func lfsr(tap, start big.Int) func() big.Int { // Keep track of next. next := &start // The generator. return func() big.Int { // Remember where we are. ret := *next // Work out next. fmt.Println("next", next.String(), "bit(0)", next.Bit(0)) // Is it currently odd? odd := next.Bit(0) // Shift right one. next = next.Rsh(next, 1) // If odd - tap! if odd != 0 { // Tap! next = next.Xor(next, &tap) fmt.Printf("Tap!", next.String()) } // Return where we were. return ret }}func main() { ten := new(big.Int) ten.SetString("10", 32) f := lfsr(*ten, *ten) for i := 0; i < 10; i++ { n := f() fmt.Println("lfsr ", n.String()) }}我得到的打印输出是:next 32 bit(0) 0lfsr 16next 16 bit(0) 0lfsr 8next 8 bit(0) 0lfsr 4next 4 bit(0) 0lfsr 2next 2 bit(0) 0lfsr 1next 1 bit(0) 1Tap! 0panic: runtime error: index out of range我做错了什么 - 为什么它看起来是对的?播放- 有趣的是 - 它输出:...next 1 bit(0) 1Tap!%!(EXTRA string=0)panic: runtime error: index out of rangegoroutine 1 [running]:math/big.nat.string(0xc010045150, 0x1, 0x5, 0x12b23d0, 0xa, ...) go/src/pkg/math/big/nat.go:819 +0x67fmath/big.nat.decimalString(0xc010045150, 0x1, 0x5, 0x1, 0x1, ...) go/src/pkg/math/big/nat.go:731 +0x8fmath/big.(*Int).String(0x7f851e0eff40, 0xc010045150, 0x1) go/src/pkg/math/big/int.go:331 +0xfemain.main() /tmpfs/gosandbox-94dce1ec_430947f1_6360662e_01c3d6ad_a7071d20/prog.go:41 +0x120这是a)略有不同,b)表明这fmt.Println("lfsr ", n.String())是失败的原因,但我并没有更接近于找出原因。添加经过实验(更改ten.SetString("10", 32)为ten.SetString("10", 10)),我现在得到:lfsr 5next 5 bit(0) 1Tap!%!(EXTRA string=0)panic: runtime error: index out of range现在去睡觉 - 希望有人能帮忙。
2 回答
- 2 回答
- 0 关注
- 173 浏览
添加回答
举报
0/150
提交
取消