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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T20:27:12+00:00 2026-06-09T20:27:12+00:00

I need to set all the low order bits of a given BigInteger to

  • 0

I need to set all the low order bits of a given BigInteger to 0 until only two 1 bits are left. In other words leave the highest and second-highest bits set while unsetting all others.

The number could be any combination of bits. It may even be all 1s or all 0s. Example:

MSB    0000 0000
       1101 1010
       0010 0111
       ...
       ...
       ...
LSB    0100 1010

We can easily take out corner cases such as 0, 1, PowerOf2, etc. Not sure how to apply popular bit manipulation algorithms on a an array of bytes representing one number.

I have already looked at bithacks but have the following constraints. The BigInteger structure only exposes underlying data through the ToByteArray method which itself is expensive and unnecessary. Since there is no way around this, I don’t want to slow things down further by implementing a bit counting algorithm optimized for 32/64 bit integers (which most are).

In short, I have a byte [] representing an arbitrarily large number. Speed is the key factor here.

NOTE: In case it helps, the numbers I am dealing with have around 5,000,000 bits. They keep on decreasing with each iteration of the algorithm so I could probably switch techniques as the magnitude of the number decreases.

Why I need to do this: I am working with a 2D graph and am particularly interested in coordinates whose x and y values are powers of 2. So (x+y) will always have two bits set and (x-y) will always have consecutive bits set. Given an arbitrary coordinate (x, y), I need to transform an intersection by getting values with all bits unset except the first two MSB.

  • 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-09T20:27:14+00:00Added an answer on June 9, 2026 at 8:27 pm

    Try the following (not sure if it’s actually valid C#, but it should be close enough):

    // find the next non-zero byte (I'm assuming little endian) or return -1
    int find_next_byte(byte[] data, int i) {
        while (data[i] == 0) --i;
        return i;
    }
    
    // find a bit mask of the next non-zero bit or return 0
    int find_next_bit(int value, int b) {
        while (b > 0 && ((value & b) == 0)) b >>= 1;
        return b;
    }
    
    byte[] data;
    
    int i = find_next_byte(data, data.Length - 1);
    // find the first 1 bit
    int b = find_next_bit(data[i], 1 << 7);
    // try to find the second 1 bit
    b = find_next_bit(data[i], b >> 1);
    if (b > 0) {
        // found 2 bits, removing the rest
        if (b > 1) data[i] &= ~(b - 1);
    } else {
        // we only found 1 bit, find the next non-zero byte
        i = find_next_byte(data, i - 1);
        b = find_next_bit(data[i], 1 << 7);
        if (b > 1) data[i] &= ~(b - 1);
    }
    
    // remove the rest (a memcpy would be even better here,
    // but that would probably require unmanaged code)
    for (--i; i >= 0; --i) data[i] = 0;
    

    Untested.

    Probably this would be a bit more performant if compiled as unmanaged code or even with a C or C++ compiler.

    As harold noted correctly, if you have no a priori knowledge about your number, this O(n) method is the best you can do. If you can, you should keep the position of the highest two non-zero bytes, which would drastically reduce the time needed to perform your transformation.

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

Sidebar

Related Questions

LaTeX newbie here. I need to set background color for all my \subsection titles.
I'm using Simple_form with Rails 3.2.1 All fine, except that I need to set
I need my application installer set the program to auto-startup for all users. Then
I need to set all of the properties of a class using the same
Need set zoom for camera preview...
I need set a cultureinfo different of others in a currency field. Metadata: [Display(ResourceType
I need Set collection, where its items will be identified by items class. Something
Need to set some attributes of button. For example Checked. I guess it is
I need to set query in my auth script, where in WHERE clause I
I need to set the color of the font every time I add text

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.