Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8035279
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T02:15:58+00:00 2026-06-05T02:15:58+00:00

I am making a program where one of the problems is that I need

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T02:15:59+00:00Added an answer on June 5, 2026 at 2:15 am

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm making a task-based program that needs to have plugins. Tasks need to have
I'm making a C program that needs to use two stacks. One needs to
I'm making a program that controls a game server. One of the functions I'm
I am making a timetabling program which does one to one matches from SubjectTeacherPeriod
I'm making a simple program in Objective-C. It has one class with a lot
Im making a program for class that manages a Hotel. I have a function
I've been making a program that uses core data. It has two entities, Medicine
I'm making a program that generates a maze and then uses bredth first search
I am making a program that takes input from files that I have called
I need some help with using integer from one activity to another. I am

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.