femto
9/7/2006 9:20:00 AM
hello all,
I'm running the SCRABBLE STEMS in ruby quiz book.
the second algorithm has the following code snipet:
hash.each do |k, v|
v = (v & 0x5555555) + ((v>>1) & 0x5555555)
v = (v & 0x3333333) + ((v>>2) & 0x3333333)
v = (v & 0xf0f0f0f) + ((v>>4) & 0xf0f0f0f)
v = (v & 0x0ff00ff) + ((v>>8) & 0x0ff00ff)
v = (v & 0x000ffff) + ((v>>16) & 0x000ffff)
count[k] = v if v >= cutoff
end
I don't get why the many lines v= ....
is equivlent to sprint("%b", v).count("1"),
ie. count the occurrence of 1 in binary v.