[rubyquiz] don't understand an algorithm

F

femto gary

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.
 
F

femto gary

Thanks , I think I got it.

it's a parallel count

f> v = (v & 0x5555555) + ((v>>1) & 0x5555555)

Imagine that v is splitted in pair of bits, after this line each pair contains
the number of ones in the two bit positions in the original v

moulon% ruby -e 'p "%b" % 0x5555555'
"101010101010101010101010101"
moulon%

f> v = (v & 0x3333333) + ((v>>2) & 0x3333333)

Each nibble contains the number of ones in the 4 bit positions

moulon% ruby -e 'p "%b" % 0x3333333'
"11001100110011001100110011"
moulon%

f> v = (v & 0xf0f0f0f) + ((v>>4) & 0xf0f0f0f)

You have the number of ones in the 8 bits positions

moulon% ruby -e 'p "%b" % 0xf0f0f0f'
"1111000011110000111100001111"
moulon%

f> v = (v & 0x0ff00ff) + ((v>>8) & 0x0ff00ff)
f> v = (v & 0x000ffff) + ((v>>16) & 0x000ffff)

and finally the total number of ones in v.


Guy Decoux
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top