[assign] operators+= and -= for set/reset bits in std::bitset

Dear All, I was a little bit tired of using string initialization of std::bitset like: bitset<8> flag_pattern(string("00111000")); What I found more readable was this: Bitset<8> flag_pattern; //initialisation to "00000000" flag_pattern += 1,3,4,5; //set bits at indices 1,3,4,5 to true: 01011100 flag_pattern -= 1,3; //set bits at indices 1,3 to false: 00001100 In that way I can specify an easy readable configuration of flags: enum { left, right, center } flag_pattern += left,right; If interest exists I could contribute the respective code for the assign library. Best regards, Bernd P.S. Please let me know if I missed some formal criteria for this post as I am new to this NG

On Tue, Dec 7, 2010 at 9:11 AM, Winkler, Bernd < Bernd.Winkler@ipa.fraunhofer.de> wrote:
Bitset<8> flag_pattern; //initialisation to "00000000" flag_pattern += 1,3,4,5; //set bits at indices 1,3,4,5 to true: 01011100
Doesn't the interpretation of that depend on the endianness of your architechture? I would have expected Bitset<8> flag_pattern; //initialisation to "00000000"
flag_pattern += 1,3,4,5; //set bits at indices 1,3,4,5 to true: 00111010
- Rob.

On 12/07/2010 01:24 AM, Robert Jones wrote:
On Tue, Dec 7, 2010 at 9:11 AM, Winkler, Bernd< Bernd.Winkler@ipa.fraunhofer.de> wrote:
Bitset<8> flag_pattern; //initialisation to "00000000" flag_pattern += 1,3,4,5; //set bits at indices 1,3,4,5 to true: 01011100
Doesn't the interpretation of that depend on the endianness of your architechture? I would have expected
AFAIK, there's nothing architecture-dependent about the semantics of std::bitset (that's what we're talking about, right?) operations. I would usually consider "set bit at indices 1,3,4,5 to true" to yield "00111010", but it depends which direction you like to write your bitset bits... - Jeff

On 7 December 2010 10:40, Jeffrey Lee Hellrung, Jr. <jhellrung@ucla.edu>wrote:
I would usually consider "set bit at indices 1,3,4,5 to true" to yield "00111010", but it depends which direction you like to write your bitset bits...
- Jeff
It should have the same effect as: bitset<8> bits; bits.set(1); bits.set(3); bits.set(4); bits.set(5); Anything else would be terribly confusing. Note that this corresponds to "00111010".
participants (4)
-
Duncan Exon Smith
-
Jeffrey Lee Hellrung, Jr.
-
Robert Jones
-
Winkler, Bernd