C++ Logo

std-discussion

Advanced search

bitset, the copy assignment operator: a bug in the description (C++ 20)

From: Vladimir Grigoriev <vlad.moscow_at_[hidden]>
Date: Fri, 27 Sep 2019 19:55:43 +0300
In the description of the copy assignment operator of the standard class std::bitset there are used a wrong logic.

20.9.2.2 Members

bitset& operator&=(const bitset& rhs) noexcept;

1 Effects: Clears each bit in *this for which the corresponding bit in rhs is clear, and leaves all other bits unchanged .

However gcc behaves differently. Consider the following demonstrative program.

#include <iostream>
#include <bitset>
int main()
{
std::bitset<3> b1( "101" );
std::bitset<3> b2( "010" );

std::cout << "b1 = " << b1 << '\n';
std::cout << "b2 = " << b2 << '\n';

b1 = b2;

std::cout << "b1 = " << b1 << '\n';
}
 
Its output is

b1 = 101
b2 = 010
b1 = 010
So the second bit was changed though the corresponding bit in b2 is not clear.

With best regards,
Vlad from Moscow
You can meet me at http://cpp.forum24.ru/ or www.stackoverflow.com or http://ru.stackoverflow.com

Received on 2019-09-27 11:57:54