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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T10:03:48+00:00 2026-05-13T10:03:48+00:00

In the following example i expected the swap of the bits. Instead the second

  • 0

In the following example i expected the swap of the bits. Instead the second bit becomes overwritten, but why and how could i achieve the expected behavior?

#include <iostream>
#include <string>
#include <algorithm>

using namespace std;

int main()
{
    bitset<2> test(string("10"));
    cout << test; // Prints "10"
    swap(test[0], test[1]);
    cout << test; // Prints "11", why not "01"?
}
  • 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-13T10:03:48+00:00Added an answer on May 13, 2026 at 10:03 am

    This is pure nasty. First we have to look at the declaration of swap:

    template<class T>
    void swap(T &left, T &right);
    

    Now, operator[]() on bitset has two overloads:

    bool operator[](size_type _Pos) const;
    reference operator[](size_type _Pos);
    

    Here reference is bitset::reference, a nested class in bitset that effectively acts as a proxy reference to the one of the underlying bits. What it encapsulates is the bitset and a position in the bitset. Because of the declaration of swap, the second overload is chosen and we are swapping two bitset::references. Now here’s where it gets nasty. Let’s look at a typical implementation of swap:

    template class<T> swap(T &left, T &right) {
        T temp = left;
        left = right;
        right = temp;
    }
    

    The problem is that left and right are both references to a bitset::reference. They have the same underlying data (because they are proxies; same meaning the both point to the same bitset!) they just encapsulate different positions in that bitset. Thus, think of it like this left is position 0 in some bitset and right is position 1 in some bitset and that bitset is the same bitset as left! Let’s forever refer to this bitset as BS (chosen intentionally).

    So,

    T temp = left;
    

    says that temp is position 0 in BS.

    left = right;
    

    sets position 0 in left to position 1 in BS (which simultaneously changes position 0 in temp!)

    right = temp;
    

    sets position 1 in right to position 0 in BS (which was just set to position 1 in BS!). So at the end of this mess have that position 0 is whatever position 1 was and position 1 is unchanged! Now, because position 0 is the LSB and position 1 is the MSB we have that “10” becomes “11”. Ugly.

    You can get around this with a template specialization:

    namespace std {
        template<>
        void swap<bitset<2>::reference>(
            bitset<2>::reference &left,
            bitset<2>::reference &right
        ) {
            bool temp = (bool)left;
            left = (bool)right;
            right = (bool)temp;
        }
    }
    

    Then:

    int main() {
        bitset<2> test(string("10"));
        cout << test; // Prints "10"
        swap(test[0], test[1]);
        cout << test; // Prints "01", hallelujah!
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was trying the following example, but with external URLs: Using WebViews The example
Given the following example (using JUnit with Hamcrest matchers): Map<String, Class<? extends Serializable>> expected
I am trying to run a boost example but I am getting the following
Consider following example : public class SomeBusinessLayerService : DataService<MyEntityContainer> { [WebInvoke] void DoSomething(string someParam)
Given the following example, why do I have to explicitly use the statement b->A::DoSomething()
Let me use the following example to explain my question: public string ExampleFunction(string Variable)
In the following example should I expect that values.size() will be called every time
For the following example: http://developer.yahoo.com/yui/examples/tabview/frommarkup_clean.html I would like to make the tabs right aligned
Consider the following example. It consists of two header files, declaring two different namespaces:
I have the following example class: Test.h: @interface Test : UIButton { NSString *value;

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.