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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T23:33:07+00:00 2026-06-01T23:33:07+00:00

Lets look at very basic implementation of Bitset . struct Bitset { bool mask[32];

  • 0

Lets look at very basic implementation of Bitset.

struct Bitset {
    bool mask[32];

    bool& operator[] (int index) {
        return mask[index];
    }
};

Now I can write

Bitset bitset;
bitset[0] = 1;
std::cout << bitset[0] << "\n";

There is possible optimization. I can use unsigned int instead of bool mask[32].

struct Bitset {
    unsigned int mask;

    bool& operator[] (int index) {
        // ??
    }
};

Is it possible to write bool& operator[] (int index) with such specification ? I think std::bitset is doing something like that but i have no idea how.

  • 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-01T23:33:08+00:00Added an answer on June 1, 2026 at 11:33 pm

    No, you can’t form a reference to anything smaller than char.

    Instead, you could return an object that’s convertible to bool, supports assignment, and knows which bit to read and write, along the lines of:

    class bit_proxy
    {
    public:
        bit_proxy(unsigned & mask, unsigned bit) : mask(mask), index(index) {}
    
        operator bool() const {return mask & (1 << index);}
        void operator=(bool bit) {mask = (mask & ~(bit << index)) | (bit << index);}
    
    private:
        unsigned & mask;
        unsigned index;
    };
    
    bit_proxy bitset::operator[](unsigned index)
    {
        return bit_proxy(mask, index);
    }
    
    my_bitmask[3] = true;     // sets bit 3
    bool bit = my_bitmask[3]; // gets bit 3
    

    As noted in the comments, you might also want some compound assignment operations to more fully emulate a reference. You might also want a separate type, containing a const reference and no assignment operators, to return from a const overload of operator[].

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

Sidebar

Related Questions

I'm a bit confused regarding the document architecture. Lets look at MSN for Mac
Let's look at this situation: private Func<int, int> callback; public SomeClass(Func<int, int> callback) {
This probably is a very very basic question but i can't seem to find
My target DB is MySQL, but lets try to look at this problem more
I need very basic help (hopefully) on an Artificial Intelligence Project. My question is
Microsoft Windows lets programmers draw GUI elements using the look and feel of the
I'm very new to greendroid and I had a quick look on its functionalities.
Let's look at this code: IList<IHouseAnnouncement> list = new List<IHouseAnnouncement>(); var table = adapter.GetData();
Let's look at the following code snippet in Java. package division; import java.math.BigDecimal; final
First, let's look at the excerpt from my CMakeLists.txt : find_package(Qt4 4.8.0 COMPONENTS QtCore

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.