AND trouble (c++, std::bitset)

Programming, for all ages and all languages.
Post Reply
User avatar
Zacariaz
Member
Member
Posts: 1069
Joined: Tue May 22, 2007 2:36 pm
Contact:

AND trouble (c++, std::bitset)

Post by Zacariaz »

when doing a conditional &:

Code: Select all

std::bitset<9> a = 7;
std::bitset<9> b = 6;

if(a & b)

or

if(a & b == b) // which is what i really want
i get the following error:
could not convert `std::operator& [with unsigned int _Nb = 9u](((const std::bitset<9u>&)((const std::bitset<9u>*)((+(((unsigned int)p) * 4u)) + ((std::bitset<9u>*)((std::bitset<9u> (*)[2])this))))), ((const std::bitset<9u>&)((const std::bitset<9u>*)((((std::bitset<9u>*)(((unsigned int)i) * 4u)) + ((std::bitset<9u>*)((std::bitset<9u> (*)[8])((C*)this)))) + 8u))))' to `bool'

i understand nothing of this.
nick8325
Member
Member
Posts: 200
Joined: Wed Oct 18, 2006 5:49 am

Post by nick8325 »

& has lower precedence than == :shock:

That is, you must write

Code: Select all

if ((a & b) == b)
instead. The code you wrote parses as

Code: Select all

if (a & (b == b))
This has bitten me before.
Post Reply