Shifts? We don't need no stinking Shifts!
[The code previously given for "algebraic right shift: does not work for a shift of more than one bit, and has been replaced using a different approach. Apologies to anyone who was confused by the previous version.]
A previous post ("Op Codes? We don't need no stinking Op Codes!") showed how to do common operations on a computer that always performed the same operation (subtract and branch if not positive) on different operands. In that post, the operations performed treated the operands as integer values. There are situations in which it is convenient to treat the operands as bit strings (or Boolean vectors) where the bits are shifted or individual bits are manipulated; multiplication, division, and extraction of substrings are among these situations. Individual bits can be isolated by a combination of left and right shifts, so shifts are the basic operations for bit string manipulation.
Read more »