Reputation: 4959
I would like to know some examples of application of circular shifts. For example, a right shift on an unsigned integer would result in a division by two. Conversely, a left shift would result in a multiplication by 2. Are there any famous/interesting properties of a circular shift on binary numbers.
Note: The example about the right/left shift is to illustrate an application of that particular operator. I am asking for similar examples for the circular shift operator/function.
Upvotes: 4
Views: 3317
Reputation: 24647
t = rand(); result = t XOR cshift(t,1)
.2 (mod (2^N - 1))
mentioned in Henrik's answer.Upvotes: 5
Reputation: 23324
A regular left shift is a multiplication by 2 (mod 2^N), where N is the number of bits in your integer type.
A circular left shift is a multiplication by 2 (mod (2^N - 1)). So this can be handy when doing arithmetic mod (2^N-1).
Upvotes: 2
Reputation: 372814
One surprising place where circular shifts show up is in the Josephus survivor problem. In this slightly morbid problem, n people stand in a circle. The first person kills the second, then the third kills the fourth, etc. This process repeats where one person kills the next until only one person is left. The question is given n people, which person survives?
Amazingly, the answer is given by doing a right circular shift on n by one bit. There's a great proof of this in Graham, Knuth, and Patashnik's book Concrete Mathematics.
Hope this helps!
Upvotes: 6