Reputation: 1716
Let's say I want to write a 16bit linear feedback shift register LFSR in Python using its native shift operator.
Thanks, Gert
Upvotes: 0
Views: 490
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