[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: Circular shift

Lloyd Linklater

1/22/2008 6:20:00 PM

Zangief Ief wrote:
> I would like to write a simple implementation of *circular shift*
> (http://en.wikipedia.org/wiki/Circ...). Thank you all for any
> ideas :)

Well, I am not sure just what you are looking for but here is at least
something until one of the sharpies replies:

reverse_this_number = 1234
puts reverse_this_number.to_s(2) # before
top_bit_set = ((2**(reverse_this_number.size * 8)) &
reverse_this_number) != 0
reverse_this_number *= 2
if top_bit_set then
reverse_this_number += 1 # set the low order bit
end
puts reverse_this_number.to_s(2) # after

output:
10011010010
100110100100

Note: the output will be missing the leading zero, but it shifts it
correctly.

This is how it would be internally:


010011010010
100110100100
--
Posted via http://www.ruby-....