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

  • Home
  • SEARCH
  • 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 7934263
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T21:35:46+00:00 2026-06-03T21:35:46+00:00

First Value: I have a binary value which is actually a compact series of

  • 0

First Value:

I have a binary value which is actually a compact series of 2-bit values. (That is, each 2 bits in the binary value represents 0, 1, 2, or 3.) So, for example, 0, 3, 1, 2 becomes 00110110. In this binary string, all I care about are the 3’s (or alternately, I could flip the bits and only care about the 0’s, if that makes your answer easier). All the other numbers are irrelevant (for reasons we’ll get into in a bit).

Second Value:

I have a second binary value which is also a compacted series of 2-bit values represented the same way. It has an identical length to the First Value.

Math:

I want the sum of the 2-bit numbers in the Second Value that have the same position as a 3 from the First Value. In other words, if I have:

First:  11000011
Second: 01111101

Then my answer would be “2” (I added the first number and the last number from “Second” together, because those were the only ones that had a “11” in the First Value that matched them.)

I want to do this in as few clock cycles as possible (either on a GPU or on an x86 architecture). However, I’m generally looking for an algorithm, not an assembler solution. Is there any way faster than masking off two bits at a time from each number and running several loops?

  • 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-03T21:35:48+00:00Added an answer on June 3, 2026 at 9:35 pm

    Sure.

     // the two numbers
     unsigned int a;
     unsigned int b;
    

    Now create a mask from a that contains ‘1’ bit at an odd position only if in a there was ’11’ ending at same position.

     unsigned int mask = a & (a >> 1) & 0x55555555;
    

    Expand it to get the ’11’ pattern back:

     mask = mask | (mask << 1);
    

    So now if a was 1101100011, mask is 1100000011.

    Then mask b with the mask:

     b = b & mask;
    

    You can then perform the addition of (masked) numbers from b in parallel:

     b = (b & 0x33333333) + ((b & 0xcccccccc) >> 2);
     b = (b & 0x0f0f0f0f) + ((b & 0xf0f0f0f0) >> 4);
     b = (b & 0x00ff00ff) + ((b & 0xff00ff00) >> 8);
     b = (b & 0x0000ffff) + ((b & 0xffff0000) >> 16);
    

    For a 32-bit number, the sum is now at the lowest bits of b. This is a commonly known pattern for parallel addition of bit fields. For larger than 32-bit numbers, you would add one more round for 64-bit numbers and two rounds for 128-bit numbers.

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

Sidebar

Related Questions

Say, i have binary protocol, where first 4 bits represent a numeric value which
I have some code that first selects an option value in a dropdown menu
I have a program which works with genetic algorithms and generates an 8-bit binary
I have a binary file (test.bin) which has 2 unsigned int values respectively 1000
How to remove the leftmost bit? I have a hexadecimal value BF Its binary
I have a cascade dropdownlist when the first change value the second change using
I have three DropDownLists. If a specific Value from my first DropDownList is chosen,
I have to insert the values in the table named First, but before inserting
Suppose I have a dropdown (select tag) with 3 values. Our first option has
I have this Xaml <SolidColorBrush x:Key={x:Static SystemColors.HighlightBrushKey} Value=#123456/> <SolidColorBrush x:Key={x:Static SystemColors.HighlightTextBrushKey} Value=White/> THe first

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.