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-...
.
Servizio di avviso nuovi messaggi
Ricevi direttamente nella tua mail i nuovi messaggi per
Re: Circular shift
Inserendo la tua e-mail nella casella sotto, riceverai un avviso tramite posta elettronica ogni volta che il motore di ricerca troverà un nuovo messaggio per te
Il servizio è completamente GRATUITO!
x
Login to ForumsZone
Login with Google
Login with E-Mail & Password