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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:29:04+00:00 2026-05-25T16:29:04+00:00

I have a c# BitArray that is fairly large (500,000) in length, and I

  • 0

I have a c# BitArray that is fairly large (500,000) in length, and I am trying to get the index of all the positive bits set in the array. currently I am achieving this by:

public int[] GetIndexesForPositives()
{
    var idIndexes = new int[GetPositiveCount + 1];
    var idx = 0;
    for (var i = 0; i < Length; i++)
        {
            if (Get(i))
            {
                idIndexes[idx++] = i;
            }
        }
    return idIndexes;
}

I create an empty array of the size of known positive bits, then i lopp over the bitarray and add the index value to the return array.

This means I have to perform 500,000 loops over the array and its not exactly fast. (takes around 15ms).

I know the BitArray uses an integer array under the covers (i used it to write the GetPositiveCount function – via an alogrithm I got off stack), I wonder if there is an algorythm to do this aswell?

  • 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-05-25T16:29:04+00:00Added an answer on May 25, 2026 at 4:29 pm

    If you are able to get a int array underlying the BitArray, this should provide much better performance:

    Assuming you don’t know the number of bits that are set:

    public static int[] GetIndexesForPositives()
    {
        var idIndexes = new List<int>();
        System.Reflection.FieldInfo field = data.GetType().GetField("m_array", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
        int[] values = field.GetValue(data) as int[];
    
        for (var i = 0; i < values.Length; i++)
        {
            int _i = values[i];
            if (_i != 0)
            {
                for (var j = 0; j < 32; j++)
                {
                    if ((_i & (1 << j)) != 0)
                    {
                        idIndexes.Add(i * 32 + j);
                    }
                }
            }
        }
        return idIndexes.ToArray();
    }
    

    If you do know the number of bits that are set you can do this instead:

    public static int[] GetIndexesForPositives(int length)
    {
        var idIndexes = new int[length];
        var idx = 0;
        System.Reflection.FieldInfo field = data.GetType().GetField("m_array", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
        int[] values = field.GetValue(data) as int[];
    
        for (var i = 0; i < values.Length; i++)
        {
            int _i = values[i];
            if (_i != 0)
            {
                for (var j = 0; j < 32; j++)
                {
                    if ((_i & (1 << j)) != 0)
                    {
                        idIndexes[idx++] = i * 32 + j;
                    }
                }
            }
    }
    

    My tests have these two working faster than your method, even the one that doesn’t know how large the return array will be in the first place.

    My results tested using a random BitArray of 50million records:

    1) 25001063 records found in 50000000, took 1415.5752ms
    2) 25001063 records found in 50000000, took 1099.67ms
    3) 25001063 records found in 50000000, took 1045.6862ms
    4) 25001063 records found in 50000000, took 745.7762ms"
    
    1) is your code but using an arraylist instead of using some `GetPositiveCount` to get the output length.
    2) is your code
    3) is my (revised) first example
    4) is my (revised) second example
    

    edit: furthermore it is worth pointing out that this is a problem that could really benefit from being made multi-threaded. Break the ByteArray up into 4 parts and there you have 4 threads that could run checking the data at once.

    Edit: I know this is already accepted but here’s another bit you can do to improve performance if you know that most of the time your list will be very sparse:

    for (var j = 0; j < 32; j++)
    {
         if (_i == 0)
             break;
         if ((_i & (1)) != 0)
         {
             idIndexes.Add(i * 32 + j);
         }
         _i = _i >> 1;
     }
    

    it is slightly slower when the list is >40% or more populated however if you know the list is always going to be 10% 1s and 90% 0s then this will run even faster for you.

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

Sidebar

Related Questions

I want to count the bits that are set in an extremely large bit-vector
I have a BitArray with the length of 8, and I need a function
I have a System.Collections.BitArray array (~3000 items) and I would like to shift all
I have a function to create a pdf which should return bitarray. Below is
I have a bit array implementation where the 0th index is the MSB of
Have you guys had any experiences (positive or negative) by placing your source code/solution
Have you managed to get Aptana Studio debugging to work? I tried following this,
we have strange problem, we have single signon and we are trying to fetch
i am trying to write content of a very large char array to the
I have the following string that I would like to Huffman-encode and store efficiently

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.