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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T15:24:12+00:00 2026-06-18T15:24:12+00:00

I have written a templates class for storing multiple bools in an integer. Right

  • 0

I have written a templates class for storing multiple bools in an integer.
Right now, setting and getting each bool is done with explicit functions

    bool isBitSet(int index)
    {
        return static_cast<bool>((block_ >> index) % 2)
    }

    void setBitOn(int index)
    {
        block_ |= 1 << index;
    }

I believe that the following would work for getting a value, but how would setting work since we can’t directly return a reference for a bit?

    const bool operator [] (int index) const 
    {
        return static_cast<bool>((block_ >> index) % 2);
    }
  • 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-18T15:24:14+00:00Added an answer on June 18, 2026 at 3:24 pm

    The same is done in std::vector<bool> and in std::bitset in the standard library. As stated in the reference, std::vector<bool> it returns a proxy class that has its operators overloaded to act as an element of the vector.

    You could to that as well.

    For a user-friendly example see again the reference for a public interface, it is something like this:

    template <class Allocator>
    class vector<bool, Allocator> {
      // ...
      public:
        class reference {
            friend class vector;
            reference();
          public:
            ~reference();
            operator bool() const;
            reference& operator=(bool x);
            reference& operator=(const reference&);
            void flip();
        };
      // ...
    };
    

    To implement this class you should store a member pointer to your actual data block and a mask to operate with.

    For a real example, in the g++ headers look for member class of std::vector<bool> called std::vector<bool>::_Bit_reference in the file bits/stl_bvector.h.


    To clarify the OP with an example:

    Let’s say you have a class containing 320 bools. You could write it as:

    class boolcontainer {
      uint32_t data[10];
    public:
      //default ctor. to initialize the elements with zeros
      boolcontainer() { for (int i = 0; i < 10; ++i) { data[i] = 0; } }
    }
    

    You want to add an operator[]. To add a const one is easy:

    class boolcontainer {
      uint32_t data[10];
    public:
      bool operator[](int i) const { return data[i/32] & (1 << (i%32)); }
    }
    

    to have a non-const one you need much more. First you need to create a class that represents a reference to your value. You must have some kind of pointer to where the value is stored and (in this case) you need a bitmask to specify one concrete bit. To be able to handle this as a bool& you need to add some operators, namely conversion to bool and operator=:

    class reference {
      uint32_t *dataptr;
      uint32_t mask;
    public:
      //constructor just initializing members
      reference(uint32_t *dataptr_, uint32_t mask_) : dataptr(dataptr_), mask(mask_) {}
    
      //conversion to bool
      operator bool() const {
        //just like in the getter, but the bitmask is stored now locally
        return *dataptr & mask;
      }
    
      //sets one single bit represented by mask to b
      reference& operator=(bool b) {
        if (b) {
          *dataptr |= mask;
        } else {
          *dataptr &= ~mask;
        }
        return *this;
      }
    
      //TODO copy ctor., operator==, operator<
    };
    

    Note that the above struct will behave as a bool& — reading from it reads the value from the data point represented by the pointer and the mask, and similarly, writing to it overwrites the bit at the represented location. I also wrote a constructor that initializes the members.

    Now all you need is that your boolcontainer’s operator[] should return an object of the above class:

    class boolcontainer {
      uint32_t data[10];
    public:
    
      boolcontainer() { for (int i = 0; i < 10; ++i) { data[i] = 0; } }
    
      class reference {
         ... //see above
      }
    
      //keep the const version for efficiency
      bool operator[](int i) const { return data[i/32] & (1 << (i%32)); }
    
      //non-const version returns our reference object.
      reference operator[](int i) { return reference(&data[i/32], 1 << (i%32)); }
    };
    

    And now some code to test it (prints only the first 40 values):

    #include <iostream>
    #include "boolcontainer.h"
    
    void printboolcontainer(const boolcontainer &bc)
    {
        //note that this is the constant version
        for (int i = 0; i < 40; ++i) {
            std::cout << bc[i];
        }
        std::cout << std::endl;
    }
    
    int main()
    {
        boolcontainer bc;
        printboolcontainer(bc);
        bc[0] = true;
        bc[3] = true;
        bc[39] = true;
        printboolcontainer(bc);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've written an INI class that loads/saves/creates an INI data format, however right now
have written this little class, which generates a UUID every time an object of
I have written a function template for serialization of enums to/from our stream class
I have a website that is written in dutch. Now I have to provide
Is it possible to have multiple versions of the same class which differ only
I have written the following wrapper for std::bind and std::queue : #include Queue.h template<class
I have written myself a form: class Autopoweroff_Form(forms.Form): autopoweroff_groups = forms.CharField(required=True) autopoweroff_groups_hosts = forms.CharField(required=True)
I have written a program that has a class with a constructor and destructor
I have written a small class Dice that imitates the behavior of real dice
I have written a simple class that wraps the JQUeryUI Dialog. It basically looks

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.