hzcya1995

n级线性反馈移位寄存器,这里以n=7为例,假设初始状态为1000000,使用C++标准库中的bitset类来实现,bitset类的用法详解:点击打开链接

 

#include <iostream>
#include <stdlib.h>
#include <bitset>
#define n 7
using namespace std;
int main()
{
	system("title = LFSR");
	//初始化n位的二进制数据,初始状态为二进制1000,及十进制8
	bitset<n>bint(64);
	bitset<n>str(bint);
	string s1,s2;//保存输出结果
	//遍历bint中的数字
	/*for (int i = bint.size()-1; i>=0; i--)
		cout << bint[i];
	cout << endl;*/
	cout << "初始状态为:"<<bint.to_string() << endl;
	//str = bint.to_string();//如果是string类型的str用这句
	//生成LFSR的状态变化
	do
	{
		int j = bint[n - 1] ^ bint[0];
		bint.operator>>=(1);//向右移1位
		//第1个数据和第n个数据异或然后赋值给第n个
		bint[n - 1] = j;
		cout << bint.to_string() << endl;
		//把bint转换成string放入s1
		s1 = bint.to_string();
		//把a1放入s2中
		s2.push_back(s1[3]);

	} 
	while (str.to_string() != bint.to_string());
	cout << "输出序列为:" <<s2<< endl;
	system("pause");
	return 0;
}

 

 

 

 

 

 

 

分类:

技术点:

相关文章:

  • 2021-04-20
  • 2021-09-09
  • 2022-12-23
  • 2021-07-15
  • 2021-11-18
  • 2021-11-18
  • 2021-11-18
  • 2021-11-18
猜你喜欢
  • 2021-08-24
  • 2021-11-18
  • 2021-04-18
  • 2021-08-23
  • 2021-09-30
  • 2021-08-09
  • 2021-11-18
相关资源
相似解决方案