Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Looping through bits in an integer, ruby

I am making a program where one of the problems is that I need to do some analysis of the bit pattern in some integers.

Because of this I would like to be able to do something like this:

#Does **NOT** work:
num.each_bit do |i|
   #do something with i
end

I was able to make something that works, by doing:

num.to_s(2).each_char do |c|
   #do something with c as a char
end

This however does not have the performance I would like.

I have found that you can do this:

0.upto(num/2) do |i|
   #do something with n[i]
end

This have even worse performance than the each_char method

This loop is going to be executed millions of times, or more, so I would like it to be as fast as possible.

For reference, here is the entirety of the function

@@aHashMap = Hash.new(-1)

#The method finds the length of the longes continuous chain of ones, minus one 
#(101110 = 2, 11 = 1, 101010101 = 0, 10111110 = 4)

def afunc(n) 
if @@aHashMap[n] != -1
    return @@aHashMap[n]
end

num = 0
tempnum = 0
prev = false

(n.to_s(2)).each_char do |i|
    if i
        if prev
            tempnum += 1
            if tempnum > num
                num = tempnum
            end
        else
            prev = true
        end
    else
        prev = false
        tempnum = 0
    end
end

@@aHashMap[n] = num
return num
end
like image 863
Automatico Avatar asked Jun 06 '12 09:06

Automatico


People also ask

How to loop a method in Ruby?

The simplest way to create a loop in Ruby is using the loop method. loop takes a block, which is denoted by { ... } or do ... end . A loop will execute any code within the block (again, that's just between the {} or do ...

Is there for loop in Ruby?

“for” loop has similar functionality as while loop but with different syntax. for loop is preferred when the number of times loop statements are to be executed is known beforehand. It iterates over a specific range of numbers.

What is a step array?

In computer programming, the stride of an array (also referred to as increment, pitch or step size) is the number of locations in memory between beginnings of successive array elements, measured in bytes or in units of the size of the array's elements.


2 Answers

To determine the length of the longest sequence of consecutive 1's, this is more efficient:

def longest_one_chain(n)
  c = 0
  while n != 0
    n &= n >> 1
    c += 1
  end
  c
end

The method simply counts how many times you can "bitwise AND" the number with itself shifted 1 bit to the right until it is zero.

Example:

                 ______ <-- longest chain
    01011011100001111110011110101010 c=0
AND  0101101110000111111001111010101
        1001100000111110001110000000 c=1, 1’s deleted
AND      100110000011111000111000000
            100000011110000110000000 c=2, 11’s deleted
AND          10000001111000011000000
                    1110000010000000 c=3, 111’s deleted
AND                  111000001000000
                     110000000000000 c=4, 1111’s deleted
AND                   11000000000000
                      10000000000000 c=5, 11111’s deleted
AND                    1000000000000
                                   0 c=6, 111111’s deleted
like image 157
Stefan Avatar answered Oct 01 '22 06:10

Stefan


Ruby might not be a good choice for your project. The strength of ruby is not it's performance but that it lets you do things like:

n.to_s(2).scan(/1+/).sort.last.length - 1

instead of writing mountains of code. Really just about any other language is likely to perform better if you don't mind writing complex code (which you don't seem to).

like image 25
pguardiario Avatar answered Oct 01 '22 04:10

pguardiario