Using: bitset( string() )

M

mathieu

Hi there,

I must be doing something wrong, but I do not understand the
documentation for bitset( string ), shouldn't this be equivalent (ref
== mask ) ?


#include <bitset>
#include <iostream>
#include <string>

int main()
{
std::bitset<8> ref( 13ul );
std::cout << ref << std::endl;

const char v[] = "1101";
std::bitset<8> mask( std::string(v) );
std::cout << mask << std::endl;
return 0;
}


Thanks
-Mathieu
 
M

mathieu

#include <bitset>
#include <iostream>
#include <string>
int main()
{
std::bitset<8> ref( 13ul );
std::cout << ref << std::endl;
const char v[] = "1101";
std::bitset<8> mask( std::string(v) );
std::cout << mask << std::endl;
return 0;
}

You've stepped into a funky corner of C++ syntax. This code declares
mask to be a function, and the compiler converts its address to a bool
to insert it into a stream. Break up the definition of mask:

std::string str(v);
std::bitset<8> mask(str);

Thanks ! That was it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

bitset in C 4
some questions about "bitset" class 2
one bitset simple question 6
Crossword 2
Character operations in C++ 2
TF-IDF 1
CIN Input #2 gets skipped, I don't understand why. 1
std::bitset in union 7

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top