Re: bitset
On 11 mrt, 19:12, Michael.Boehni...@gmail.com wrote:
On 11 Mrz., 08:44, "gast...@hotmail.com" <gast...@hotmail.com> wrote:> I was using std::bitset in this manner:
[..]
Your problems arise from your definition of the enum items. You'd be
better off using the index positions of the corresponding bits, or a
second enum with these values.
enum EBit_idx { eBti0 = 0, eBti1, eBti2, eBti3 };
enum EBit { eBt0 = 1 << eBti0, eBt1 = 1 << eBti1, eBt2 = 1 << eBti2,
eBt3 = 1 << eBti3 };
std::bitset<4> bts( eBt0 | eBt2 );
std::bitset<4> bts( ( 1 << eBti0 ) | ( 1 << ebti2 ) ); // alternative.
In terms of runtime performance both are identical as the inner
expression is evaluated during compilation.
To test a bit you can use, e.g.
bool test = bts.test( eBti2 );
bool test = ( ( bts & eBt2 ) != 0 ); // alternative 1
bool test = ( ( ( 1 << eBti2 ) & bts ) != 0 ) // alternative 2
The first one looks best for me, but the other ones are identical in
runtime behavior.
{ Edits: quoted signature and clc++m banner removed. Please don't quote
extraneous material. -mod }
I was thinking that myself. However I used the bitwise exclusive
enums, so that a constructor could take multiple arguments. This is
essential, otherwise as alternative I could also take a vector and
push_back the values:
class Foo
{
Foo(const std::bitset<4>& cr);
};
Foo foo(eBti0 | eBti1);
and there was my surprise in asymmetric behavior: the constructor
takes the 'values' and translates that back to positions, while the
rest of the class interface works with the position only.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]