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

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

Members online

Forum statistics

Threads
473,989
Messages
2,570,207
Members
46,782
Latest member
ThomasGex

Latest Threads

Top