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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T19:34:35+00:00 2026-06-15T19:34:35+00:00

I need to read data from a stream using the following algorithm: -Count all

  • 0

I need to read data from a stream using the following algorithm:

-Count all consecutive set bits (“1″s) from the stream.

-Then, read k more bits from the stream. K is variable and changes throughout the program. Lets call the read data “m”

The decoded number then is

number = (consecutive_set_bits << k) + m;

This algorithm is executed a very large amount of times. Because of this, it is crucial that this piece of code be as fast as possible.

The main problem is that the number of coded numbers in a 1byte, two byte, four byte, etc. set is variable, and thus a trivial implementation (the only one that I have in my head right now) requires a loop that reads single bits from the stream. In the worst case, I have 14 iterations through the loop for just one coded coefficient.

Can I avoid this loop somehow?

  • 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-15T19:34:36+00:00Added an answer on June 15, 2026 at 7:34 pm

    The idea of sequentially extracting single bits is not too bad. If done right, it may be almost as fast as any other solution.

    Bit sequences at arbitrary positions in a stream of granularity g, with g=16 for a stream of (16-bit) words for instance, can be handled block-wise on blocks of size g.

    To extract the bits at positions s through e (with (e - s) <= g) from a stream as a ‘right-aligned’ number an example implementation may be:

    shift = s % g
    
    lowerBits = data[ floor( s / g ) ] >> shift
    upperBits = data[ floor( e / g ) ] << (g - shift)
    
    bitSequence = (lowerBits | upperBits) & ( (1 << (e-s)) -1 )[*]
    

    [*] this last term only masks out any unneeded upper bits we may have got and makes them 0 in the final result.

    (careful with the endianess of your data :))

    Whether this will really speed things up or not cannot be determined in general. (Depends on the data being processed, the underlying computing hardware, the compiler used &c. Note that some divisions and one modulo operation are required which might slow down the algorithm significantly.)

    Extracting bits one-by-one can be done quite efficiently in the same manner. For example:

    blockIndex = floor( bitPosition / g )
    bitIndex = bitPosition % g
    nextBit = (data[ blockIndex ] >> bitIndex) & 1
    

    This can of course be optimized to avoid the re-calculation of blockIndex and bitIndex if and when the bitPosition is always only incremented by 1.

    Another common approach is to use a variable ‘mask’ to extract the single bits:

    mask = 1
    index = 0
    while ( not all bits read ) { 
      block = data[index]
      if ( mask & block != 0 ) {
        // a 1 was encountered
      } else {
        // a 0 was encountered
      }
      mask = mask << 1
      if ( mask == 0 ) {
        mask = 1
        index = index + 1
      }
    }
    

    Note how the mask is used to both mask the current bit and keep track of when to advance to the next block of data. For this to work, mask must of course be of the same width g as the data blocks.

    To sum it all up:

    I don’t think, in a general case, the solution can be more efficient than one loop iteration per bit read and any optimizations will only slightly change the performance in one direction or the other.

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

Sidebar

Related Questions

I need to read from a continuous data stream (pipe, actually), line by line,
I need to read data from XML to a List<>. The XML file contains
I need to read data from a legacy database file produced by Visual Basic
As part of an assignment, I need to read data from a binary file
Hi there I'm use to SQL, but I need to read data from a
I need to read the data from a file that can be either comma
I need to read some data from an input file and plot a graph
I need code for read json data from url in ios (objective c) If
I need to read small sequences of data from a 3.7 GB file. The
I need to write a script in Matlab, which will read some data from

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.