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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T06:00:21+00:00 2026-05-16T06:00:21+00:00

Imagine two bitmasks, I’ll just use 8 bits for simplicity: 01101010 10111011 The 2nd,

  • 0

Imagine two bitmasks, I’ll just use 8 bits for simplicity:

01101010
10111011

The 2nd, 4th, and 6th bits are both 1. I want to pick one of those common “on” bits at random. But I want to do this in O(1).

The only way I’ve found to do this so far is pick a random “on” bit in one, then check the other to see if it’s also on, then repeat until I find a match. This is still O(n), and in my case the majority of the bits are off in both masks. I do of course & them together to initially check if there’s any common bits at all.

Is there a way to do this? If so, I can increase the speed of my function by around 6%. I’m using C# if that matters. Thanks!

Mike

  • 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-16T06:00:22+00:00Added an answer on May 16, 2026 at 6:00 am

    If you are willing to have an O(lg n) solution, at the cost of a possibly nonuniform probability, recursively half split, i.e. and with the top half of the bits set and the bottom half set. If both are nonzero then chose one randomly, else choose the nonzero one. Then half split what remains, etc. This will take 10 comparisons for a 32 bit number, maybe not as few as you would like, but better than 32.

    You can save a few ands by choosing to and with the high half or low half at random, and if there are no hits taking the other half, and if there are hits taking the half tested.

    The random number only needs to be generated once, as you are only using one bit at each test, just shift the used bit out when you are done with it.

    If you have lots of bits, this will be more efficient. I do not see how you can get this down to O(1) though.

    For example, if you have a 32 bit number first and the anded combination with either 0xffff0000 or 0x0000ffff if the result is nonzero (say you anded with 0xffff0000) conitinue on with 0xff000000 of 0x00ff0000, and so on till you get to one bit. This ends up being a lot of tedious code. 32 bits takes 5 layers of code.

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

Sidebar

Related Questions

Imagine two simple java applications. Both of them are implementing the same JAR file
Imagine you have two defined routes: map.resources articles map.resources categories, :has_many => :articles both
Imagine I have two very complex but identical objects in c#, and I want
I will try and explain exactly what I want to achieve first. Imagine two
Imagine two classes which share almost the same exact methods and properties, both extending
Imagine two positive integers A and B. I want to combine these two into
The Situation: Imagine two servers A and B. Server B just outputs a PDF
I imagine two, but I would like to confirm if I am making sense
Imagine you have two views with code like the following: controller_a/a.html.erb <%= content_tag(:div) do
Imagine you have two images A and B, and a third grayscale image T.

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.