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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:01:23+00:00 2026-05-23T15:01:23+00:00

How do I use BitArray to do a few things such the most basic,

  • 0

How do I use BitArray to do a few things such the most basic, setting a value to bits, not just bits! I am starting to regret ever using this crap called BitArray.

Say I got bits such as this.

public enum RULE
{
    NOTHING = 0x0,
    FIRST_STEP = 0x1,
    FOO_YOU = 0x2,
    BAR_FOO = 0x10,
    FOO = 0x20,
    BAR = 0x40,
    FOO_BAR = 0x80,
    READY = 0x100,
    ...//LOTS MORE BITS
    FINAL_FLAG_BIT= 0x10000000 //Final bit.. uses the 29th bit.
};

Now say I do this..

//only use 29 bits to save memory, probably still uses 4 bytes lol.
BitArray rules= new BitArray(29);

//As you can see what I tried to do.
public bool ruleEnabled(RULE i)
{
    return rules[(int)i]; //<- this is impossible as it sets BITS not bitmasks.
}

public void setRule(RULE rule, bool b) {
    rules.Set((int)rule, b);
}

So I wasted around 30 minutes implementing this without knowing one of it’s many limitations.. You know there isn’t even any way to cast it down to it’s value.. without using CopyTo

So I ended up using just 1 variable (seems this solution is both cleaner and faster) and only had to alter 2 methods setRule and ruleEnabled for it to work properly.

private int rules; //uses only 29 of the 32 bits.
public bool ruleEnabled(RULE i)
{
    int bits = (int)i;
    return (rules & bits) == bits;
}

public void setRule(RULE rule, bool set) {
    if (set)
        rules |= (int)rule;
    else
        rules &= ~(int)rule;
}

My question is did I do the right thing? and why is the BitArray ever useful for? if it has so much limitations.. you can do all of it’s operations such as AND OR NOT XOR already with & | ~ ^

I guess BitArray is best used when you are dealing with more bits then any datatype can represent.. for compressions/encryptions etc..

  • 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-23T15:01:23+00:00Added an answer on May 23, 2026 at 3:01 pm

    BitArray is accessed by index, not by flags. For instance, for a bit array with length 29, the only possible indexes range from 0 through 28. Thus, the following is invalid:

    rules[RULE.READY] = true; // where READY is equal to 0x100, but the
                        // bit array's length is only 29.
    

    To make it work as intended, you must convert the flag to an index first. The following function may help:

    public static int FlagToIndex(int flag){
       int i=0;
       if(flag==0)return i;
       while((flag&1)==0){
         flag>>=1;
         i++;
       }
       return i;
    }
    

    With this function you can now properly index the bit array:

    rules[FlagToIndex((int)RULE.READY)] = true;
    

    I hope this helps.

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

Sidebar

Related Questions

Use case: I've just entered insert mode, and typed some text. Now I want
Use default: <input type=radio name=restype value=resdef onfocus=document.getElementById('resupload').disabled = true; document.getElementById('resadres').disabled = true; checked=checked /><br
Use AES/Rijndael or any symmetric encryption. Encrypt the hidden value using itself as the
Use: The user searches for a partial postcode such as 'RG20' which should then
'''use Jython''' import shutil print dir(shutil) There is no, shutil.move, how does one move
Use case: A does something on his box and gots stuck. He asks B
Use case: 3rd party application wants to programatically monitor a text file being generated
Use case: user clicks the link on a webpage - boom! load of files
use LWP::Simple; use Parallel::ForkManager; @links=( [http://prdownloads.sourceforge.net/sweethome3d/SweetHome3D-2.1-windows.exe,SweetHome3D-2.1-windows.exe], [http://prdownloads.sourceforge.net/sweethome3d/SweetHome3D-2.1-macosx.dmg,SweetHome3D-2.1-macosx.dmg], [http://prdownloads.sourceforge.net/sweethome3d/SweetHome3DViewer-2.1.zip,SweetHome3DViewer-2.1.zip], ); # Max 30 processes for
use this website a lot but first time posting. My program creates a number

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.