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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T23:29:46+00:00 2026-06-11T23:29:46+00:00

In my project I need to AND two binary array of size 40 bytes(320

  • 0

In my project I need to AND two binary array of size 40 bytes(320 bits) and then compute set bit count in C++. I found some algorithms to do this but I want to know what is the fastest way of implementing it in c++. I mean what c++ data type would be proper?(unsinged char*,unsigned int 32,u_int64,…). I know many algorithms are compatible with 32bit integer although my array size is 40 bytes .

what about the algorithms described in this link:
Fast Bit Counting Techniques which one is faster?

Is const type better or there is no difference?

Any help would be much appreciated.

  • 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-11T23:29:48+00:00Added an answer on June 11, 2026 at 11:29 pm

    Here’s a version that goes through the array with 4 bytes at once, requiring 10 iterations:

    uint32_t *arr1_int = (uint32_t*) arr1;
    uint32_t *arr2_int = (uint32_t*) arr2;
    int i;
    int bits_set = 0;
    
    for (i = 0; i < 10; i++) {
        uint32_t v = arr1_int[i] & arr2_int[i];
    
        /* http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel */
        v = v - ((v >> 1) & 0x55555555);                   
        v = (v & 0x33333333) + ((v >> 2) & 0x33333333);    
        bits_set += ((v + (v >> 4) & 0xF0F0F0F) * 0x1010101) >> 24;
    }
    

    You can do this a lot faster with a modern CPU, using compiler intrinsics. For example on a 64 bit CPU with Visual C++:

    #include <intrin.h>
    
    __int64 *arr1_int = (__int64*) arr1;
    __int64 *arr2_int = (__int64*) arr2;
    int bits_set = 0;
    
    /* 40 / 8 bytes == 5 iterations */
    bits_set += __popcnt64(*arr1_int++ & *arr2_int++);
    bits_set += __popcnt64(*arr1_int++ & *arr2_int++);
    bits_set += __popcnt64(*arr1_int++ & *arr2_int++);
    bits_set += __popcnt64(*arr1_int++ & *arr2_int++);
    bits_set += __popcnt64(*arr1_int++ & *arr2_int++);
    

    But this is all with performance in mind, if you simply want some readable code that works definitely go with what Rob suggested.

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

Sidebar

Related Questions

For a project of mine, I need two properties to be set at the
I have a Maven multi-module project and I need two different parent POMs in
Need to add two same name .csproj class libraries in my solution.Have two project
So I'm doing this project for my OO class. We need to create two
I am working on a project using Maven for which I need two new
I have a Maven project within which I need execute two code generation steps.
I'm currently working on a project for which I need two sliders.. Currently I'm
For my project i need to draw a path between two points on google
I have just solved problem23 in Project Euler, in which I need a set
I am doing a project in which i need two imageviews in single layout

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.