Gert Gottschalk
Gert Gottschalk

Reputation: 1716

Using shift operator for LFSR in python

Let's say I want to write a 16bit linear feedback shift register LFSR in Python using its native shift operator.

  1. Does the operator itself have a feature to specify the bit to shifted into the new MSB position?
  2. Does the operator have a carry flag or the like to catch the LSB falling out of the register?
  3. Has to setup the register to 16bit size? Not sure how to do this in Python where variables are not clearly typed.
  4. What's the smartest way to compute the multi-bit XOR function for the feedback. Actual bit extraction or lookup table?

Thanks, Gert

Upvotes: 0

Views: 490

Answers (1)

Dietrich Epp
Dietrich Epp

Reputation: 213448

Python does not have registers and you cannot declare the type of anything.

The shift operators operate on unlimited-precision integers. If you shift left, the number will continue to get larger indefinitely (or until out of memory). If you shift right, the least-significant bit is dropped as you would expect. There is no "carry flag", that's the kind of thing you see in assembly language and Python is not assembly. Since the integers have unlimited precision, logical and arithmetic shifts are equivalent, in a sense (if you imagine that the sign bit repeats indefinitely).

Any time you want fixed width operations you will just have to mask the results of the unlimited-precision operations.

As for the "smartest" way to do something, that's not really an appropriate question for Stack Overflow.

Upvotes: 1

Related Questions