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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T19:20:39+00:00 2026-05-24T19:20:39+00:00

Is using bitset::operator[] equivalent to using bitset::test or is there some underlying optimization? That

  • 0

Is using bitset::operator[] equivalent to using bitset::test or is there some underlying optimization?

That is, are these two loops equivalent?

Using bitset::operator[]:

static const int UP = 0;
static const int DOWN = 1;

for(int i = 1; i < KEY_MAX; ++i) {
    if(_handler && (_prevKey[i] == UP && _curKey[i] == DOWN)) {
        _handler->EnqueueEvent(new KeyboardKeyDownEvent(i));
    }
    if(_handler && (_prevKey[i] == DOWN && _curKey[i] == DOWN)) {
        _handler->EnqueueEvent(new KeyboardKeyPressEvent(i));
    }
    if(_handler && (_prevKey[i] == DOWN && _curKey[i] == UP)) {
        _handler->EnqueueEvent(new KeyboardKeyUpEvent(i));
    }
}

Using bitset::test():

static const bool UP = false;
static const bool  DOWN = true;

for(int i = 1; i < KEY_MAX; ++i) {
    if(_handler && (_prevKey.test(i) == UP && _curKey.test(i) == DOWN)) {
        _handler->EnqueueEvent(new KeyboardKeyDownEvent(i));
    }
    if(_handler && (_prevKey.test(i) == DOWN && _curKey.test(i) == DOWN)) {
        _handler->EnqueueEvent(new KeyboardKeyPressEvent(i));
    }
    if(_handler && (_prevKey.test(i) == DOWN && _curKey.test(i) == UP)) {
        _handler->EnqueueEvent(new KeyboardKeyUpEvent(i));
    }
}
  • 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-24T19:20:39+00:00Added an answer on May 24, 2026 at 7:20 pm

    From the C++03 standard, §23.3.5.2/39-41:

    bool test(size_t pos) const;
    

    Requires: pos is valid
    Throws: out_of_range if pos does not correspond to a valid bit position.
    Returns: true if the bit at position pos in *this has the value one.

    §23.3.5.2/46-48:

    bool operator[](size_t pos) const;
    

    Requires: pos is valid.
    Throws: nothing.
    Returns: test(pos).

    §23.3.5.2/49-51:

    bitset<N>::reference operator[](size_t pos);
    

    Requires: pos is valid.
    Throws: nothing.
    Returns: An object of type bitset<N>::reference such that (*this)[pos] == this-
    test(pos)
    , and such that (*this)[pos] = val is equivalent to this->set(pos, val).

    So when the object is const, they return the same value, excepting that when pos is invalid test throws out_of_range while operator[] throws nothing. When the object is not const, the operator returns a proxy object allowing one to mutate the object’s data.

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

Sidebar

Related Questions

Using ASP.NET MVC there are situations (such as form submission) that may require a
Is there any performance loss/gain using bitset in place where hand written? How to
Is there a convenient analog of std::bitset<> that's dynamically sizable at instantiation time, but
Using C# .NET 3.5 and WCF, I'm trying to write out some of the
Using TortoiseSVN against VisualSVN I delete a source file that I should not have
Using C# and System.Data.SqlClient, is there a way to retrieve a list of parameters
Using C#, I need a class called User that has a username, password, active
I convert a character to binary using std::bitset<CHAR_BIT> binary('c'); but this doesn't work for
i have tried to implement following code #include <iostream> #include <bitset> using namespace std;
I have a class with a static std::map member variable that maps char s

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.