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

The Archive Base Latest Questions

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

I need a bitset with a slightly diffrent behavior when asigning variables with integer

  • 0

I need a bitset with a slightly diffrent behavior when asigning variables with integer type to a specific bit. The bit should be set to zero if the assigned integer is smaller then one, and to one elsewise.

As a simple solution I copied the STL bitset, replaced the classname with altbitset, adjusted namespaces and include guard and added following function under reference& operator=(bool __x) in the nested reference class:

template <typename T> 
reference& operator=(T i) {
    if (i<1) return operator=(false);
    return operator=(true);
}

It works as expected.

Question is if there is a better way doing this.

  • 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:20:35+00:00Added an answer on June 18, 2026 at 3:20 pm

    You shouldn’t copy a library just to add a new function. Not only that, the new function is wildly unintuitive and could possibly be the source of errors for even just reading the code, let alone writing it.

    Before:

    bv[n] = -1; // I know a Boolean conversion on -1 will take place
    assert(bv[n]); // of course, since -1 as a Boolean is true
    

    After:

    bv[n] = -1; // I guess an integer < 1 means false?
    assert(bv[n]); // Who changed my bitvector semantics?!
    

    Just write it out so it makes sense in your domain:

    bv[n] = (i < 1);
    

    Remember: simplest doesn’t always mean fewest characters, it means clearest to read.


    If you do want to extend the functionality of existing types, you should do so with free functions:

    template <typename BitSet, typename Integer>
    auto assign_bit_integer(BitSet& bits, const std::size_t bit, const Integer integer) ->
        typename std::enable_if<std::is_integral<Integer>::value,
                                typename BitSet::reference>::type
    {
        return bits[bit] = (integer < 1);
    }
    

    Giving:

    std::bitset<8> bits;
    
    assign_bit_integer(bits, 0, 5);
    // ERROR: assign_bit_integer(bits, 0, 5.5);
    

    But for such a small function with no clear “obvious” name that describes what it does concisely(assign_bit_true_if_less_than_one_otherwise_false is verbose, to say the least), just write out the code; it says the same thing anyway.

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

Sidebar

Related Questions

Need to set some attributes of button. For example Checked. I guess it is
I have a function where i use bitset.Now i need to convert it to
Need to find out the type of an element inside a listBox, whether the
Need to be able to set server(s) that replicate all information, as a master
From http://www.cplusplus.com/reference/stl/bitset/ : Because no such small elemental type exists in most C++ environments,
In the scaladoc, BitSet is defined as extending Set[Int] . So I thought using
I have a need for a BitSet which allows easy concatenation of multiple BitSets
I currently have 16 bits I want to set to variables (2 separate bytes).
I am new to perl and I need some help with the bit manipulation
I need a function to generate random integers. (assume Java long type for now,

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.