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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T19:59:03+00:00 2026-05-30T19:59:03+00:00

I want to count the bits that are set in an extremely large bit-vector

  • 0

I want to count the bits that are set in an extremely large bit-vector (i.e. 100,000 bits).

What I am currently doing is using a pointer to char (i.e. char *cPtr) to point to the beginning of the array of bits. I then:

1. look at each element of the array (i.e. cPtr[x]),   
2. convert it to an integer (i.e. (int) cPtr[x])   
3. use a 256 element look-up table to see how many bits are set in the given byte (i.e. cPtr[x]). 

It occurs to me that if I use a short int pointer instead (i.e. short int * sPtr), then I will only need half as many look-ups, but with a 65534 element look-up table, which will have its own cost in memory usage.

I’m wondering what is the optimal number of bits to examine each time. Also, if that number is not the size of some preset type, how can I walk down my bit-vector and set a pointer to be ANY arbitrary number of bits past the starting location of the bit array.

I know there are other ways to count bits, but for now I want to be certain I can optimize this method before making comparisons to other methods.

  • 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-30T19:59:04+00:00Added an answer on May 30, 2026 at 7:59 pm

    I’m wondering what is the optimal number of bits to examine each time

    The only way to find out is to test. See this question for a discussion of the fastest way to count 32 bits at a time.

    Also, if that number is not the size of some preset type, how can I
    walk down my bit-vector and set a pointer to be ANY arbitrary number
    of bits past the starting location of the bit array.

    You can’t set a pointer to an arbitrary bit. Most machines have byte-addressing, some can only address words.

    You can construct a word starting with an arbitrary bit like so:

    long wordAtBit(int32_t* array, size_t bit)
    {
        size_t idx = bit>>5;
        long word = array[idx] >> (bit&31);
        return word | (array[idx+1] << (32 - (bit&31));
    }
    
    • 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 number of set bits in a uint in Specman:
I know that to count the number of set bits in a number, the
I have a 32 Bit number and want to count know how many bits
Possible Duplicate: Best algorithm to count the number of set bits in a 32-bit
I want to count the number of bits in a binary number that are
I have a A=set of strings and a B=seperate string. I want to count
I want to count the number of rows in my table that are not
I want to count how many times i built on VS.NET . I have
I want to count the number of accounts from the resulting table generated from
I want to count how many members of an iterable meet a given condition.

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.