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 8419871
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T02:44:07+00:00 2026-06-10T02:44:07+00:00

Maybe you can help me with the following problem that can help me speed

  • 0

Maybe you can help me with the following problem that can help me speed a memory manager I am thinking of (I am not sure a solution exists – I did not find one).

I have a 32 bits register and I need to find if there are n consecutive set bits in it, and if so what is their offset. For example if the register holds the following value 111100000000000000000001111111000 and n equals to 4 – any of the following answer is accepted (offsets starts from 0):

3, 4, 5, 6, 28

The atomic operations I have are all the regular bitwise operations (&, |, ~, …) and also finding the least significant bit offset (3 in the register above). The algorithm (assuming one exists) – should take no more than 5 atomic operations.

  • 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-10T02:44:08+00:00Added an answer on June 10, 2026 at 2:44 am

    for every possible byte value (0-255) calculate the number of bits at the beginning, the number of bits at the end and the longest number of consecutive bits inside the byte and the offset of this sequence. For instance, for 0b11011101, there are 2 bits at the beginning, 1 bit at the end and a sequence of 3 consecutive bits in it.

    Store this values in 4 arrays, for instance start, end, longest, longest_offset.

    Then, consider the 32bit number as a 4 bytes array and iterate over these bytes as follows:

    int search_bit_sequence(uint32 num, int desired) {
      unsigned char *bytes = (unsigned char *)#
      int i, acu;
      for (acu = i = 0; i < 4; i++) {
        int byte = bytes[i];
        acu += start[byte];
        if (acu >= desired)
          return (i * 8 - (acu - start[byte]));
    
        if (longest[byte] >= desired)
          return ( i * 8 + longest_offset[byte]);
    
        if (longest[byte] < 8)
          acu = end[byte];
      }
      return -1; /* not found */
    }
    

    update: notice that the endianess of your CPU may require changing the loop direction.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem with preg_match_all that maybe you can help. I have a
I have the following problem, maybe someone can help me (or explain, where my
Maybe you can help me with a problem I am having. The URL of
I'm looking for a jQuery plugin that can help me with the following: I
I'd really like some help with the following MySQL / PHP problem (maybe bug?)
I have a little dilemma that maybe you can help me sort out. I've
I have run a weird problem. Maybe someone here can help me. My SQL
Maybe somebody can help me. Starting with a CSV file like so: Ticker,Price,Market Cap
Maybe you guys can help: I have a variable called $bio with bio data.
I'm learning Ruby right now and I got stuck, maybe you guys can help

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.