Vous êtes sur la page 1sur 1

How to create a LFSR in c++ for different polynomials Ask Question

asked 2 years, 8 months ago

viewed 2,919 times

active 2 years, 8 months ago

I'm trying to learn how right >> and left << shift operations in c++. I've reading some articles in
internet and some topics here, but I'm still confused. I'm trying to code a LFSR (Linear Feedback 122 People Chatting
1 Shift Register) based on users input who should give the length, seed and polynomial tap position
as input to the LFSR code. JavaScript
2 hours ago - rlemon
The code would be like this:

#include <iostream>
#include <string> Python
#include <cmath> 2 hours ago - Kevin

using namespace std;

int main()
{ Related
string seed;
unsigned int length, pos; 1697 What is the difference between #include
<filename> and #include “filename”?
cout << "Type the length and the seed" << endl;
cin >> length >> seed; 1947 How do you set, clear, and toggle a single
cout << "Polynomial tap positions" << endl; bit?
cin >> pos;
2478 What are the differences between a pointer
//Creating array with the LFSR size variable and a reference variable in C++?
unsigned int *sizee = new unsigned int[length];
//Convert the seed from char to int 1150 What are bitwise shift (bit-shift) operators
for (unsigned int i = 0; i < length; i++) { and how do they work?
sizee[i] Questions
= seed[i] - Developer
'0'; Jobs Tags Users Search… Log In Sign Up
} 1436 How can I profile C++ code running in
//Shifting Linux?
unsigned int seq = std::pow(2,length)-1;
for (unsigned int i = 1; i <= seq ; i++) { 4254 The Definitive C++ Book Guide and List
//Shift Operation here
//Show user the value 1143 What is the effect of extern “C” in C++?
}
7502 What is the “-->” operator in C++?
delete[] sizee;
1138 Easiest way to convert int to string in C++
return 0;
} 1476 C++11 introduced a standardized memory
model. What does it mean? And how is it
How can I shift bits, for example, the seed 00001 in a LFSR of length = 5 and Tap positions (Xor going to affect C++ programming?
positions) 5 and 3 (x^5+x^3+1) to the right? I expect to obtain something like: 00001 > 10000 >
01000 > 00100 > 10010, and so on, until the end of the loop, considering Fibonacci as architecture Hot Network Questions
type.
How old is the oldest light visible from Earth?
c++ bit-manipulation bit-shift
Does sslstrip work on websites only which uses
both HTTP and HTTPS?
share improve this question edited Jul 24 '15 at 18:37 asked Jul 24 '15 at 16:23
How do I explain a unicorn discharging powerful
Leonardo Alves electricity at a distance?
17 1 9
How to install two applications/packages
simultaneously?
Can you elabortate for users who don't know what a LFSR is? Surely, your problem is more basic than LFSR. –
Booming Blade trigger timing
Walter Jul 24 '15 at 16:40
How to override an unpluggable parent theme
1 std::pow(2,length) makes me sad – harold Jul 24 '15 at 16:41
function?
@Walter LFSRs are using to generate pseudo-random numbers "automatically", you can find more explanation Does writing matter a lot in research?
here: en.wikipedia.org/wiki/Linear_feedback_shift_register. Harold, I don't get your point. Im new to c++, sorry.
– Leonardo Alves Jul 24 '15 at 18:21 How resilient is the Staff of the Magi?

@LeonardoAlves you're doing a bitshift using floating point exponentiation, on the "silly scale" that's just one Can wind instruments be played out of tune?
step better than converting to a string in binary, appending zeroes, and then parsing it back – harold Jul 27 '15
Why should a software QA engineer need to learn
at 18:05
JavaScript?

add a comment Isn't C1 and C2 nulling the input signal?

Does C++11, 14, 17 or 20 introduce standard


2 Answers active oldest votes constant of pi?

Find prime factors in C++

Implementing a DNA codon table in C


If you want to encode an LFSR in an integer on a computer, you need to first understand the
representations used by LFSRs and integers. There are two important issues/differences: Theresa May refused to join strike on Syria. What
is the aim of this refusal?
0
LFSRs usually number their bits starting with 1, with a tap at bit i corresponding to having xi in What methods of transporting vehicles could be
the polynomial used to get them to an island surrounded by sharp
rocks in shallow water?
integers usually number their bits starting with 0, with bit i corresponding to a value of 2i
How can I give out my telephone number to my
LFSRs are traditionally shown with bit 1 on the left, and the highest bit on the right neighbors without implying anything?

integers are usually written in big-endian form, with bit 0 on the right and the highest bit on the Second argument of Internal`DoubleToString
left Up go the bits!

Those lead to two important things when you use integers for LFSRs: Converting succession type from Gavelkind to
Primogeniture in early game
bit i of the LFSR becomes bit i-1 of the integer How can you run a program that is bigger than
RAM?
the right shift of the LFSR becomes a left shift of the integer.
How can I list running bash scripts by the name of
so your basic LFSR step becomes: the script?

New "professional" branding perceived as "dull"


seed = (seed << 1) | parity(seed | polynomial)
Steaming with oil instead of water
where seed is the contents of the LFSR (plus extra bits shifted out previously when your integer
size is larger than your LFSR length), polynomial is the tap bits -- an integer with bit i-1 set for each question feed
xi in the polynoimal, and parity is a function that computes the xor of all the bits in an integer --
can be done with flag tricks or a single instruction on most CPUs, but there's no easy way to express
it in C.

share improve this answer answered Jul 24 '15 at 17:31


Chris Dodd
74.5k 4 75 150

I got it. But, what I really trying to understand is what (seed << 1) does. It moves the value 1 to left seed
times? – Leonardo Alves Jul 24 '15 at 18:32

1 @LeonardoAlves: (seed << 1) shifts the value from seed 1 bit position to the left. So bit 1 comes from bit
0 of seed, bit 2 from bit 1, etc. A 0 is shifted in to bit 0. – Chris Dodd Jul 24 '15 at 18:35

Oh, I got it. Thank you @Chris – Leonardo Alves Jul 24 '15 at 18:43

add a comment

First off, you will not be able to use the >> and << operators as you have an array of ints.

The << and >> operators are only defined for numeric data types. You should switch to using just an
0
int.

With a single int your algorithm for a LFSR becomes:

Calculate your tap bit: tapbit = necessary bits XORed ( ^ ) together


// ^ Note this will require shifts as well, I'd reccomend a for loop through all your taps

Shift register to the right: seed = seed >> 1;


Set the leftmost bit to the calculated tap bit: seed = seed | ( tapbit << length );

share improve this answer answered Jul 24 '15 at 16:38


DweebsUnited
108 9

Thats what I have in mind. But, what would be the seed type? and how would I define "the positions" to XOR
them? – Leonardo Alves Jul 24 '15 at 18:47

add a comment

Your Answer

Sign up or log in Post as a guest

Sign up using Google Name

Sign up using Facebook Email

required, but never shown


Sign up using Email and Password

Post Your Answer

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged c++ bit-manipulation bit-shift or
ask your own question.

STACK OVERFLOW STACK OVERFLOW COMPANY STACK EXCHANGE Blog Facebook Twitter LinkedIn
BUSINESS NETWORK
Questions About
Talent Technology
Jobs Press
Ads Life / Arts
Developer Jobs Directory Work Here
Enterprise Culture / Recreation
Salary Calculator Legal
Science
Help Privacy Policy site design / logo © 2018 Stack Exchange Inc;
Other user contributions licensed under cc by-sa 3.0
Mobile Contact Us with attribution required. rev 2018.4.9.29787

Vous aimerez peut-être aussi