
%\smallskip
%\noindent \textbf{bitset example}
\subsection{bitset example}

%012345678901234567890123456789012345678901234

\lstset{language=C++}
\begin{lstlisting}
#include <bitset>
#include <iostream>
#include <sstream>

using namespace std;

// removes leading c characters from string s
string strip(char c, string s) {
  return s.substr(
	 s.find_first_not_of(c) );
}

template<typename B>
string strip(B b) {
    stringstream ss; ss << b;
    return strip('0', ss.str());
}

int main() {
	// left-padded with zeroes
    cout << bitset<16>(43) << endl;
 	// remove left-padding
    cout << strip(bitset<64>(43)) << endl;

    bitset<10> b(43);
    b;            // 0000101011
    b.count();    // 4
    ~b;           // 1111010100
    (~b).count(); // 6
    (b << 2);     // 0010101100
    b.set();      // 1111111111
    b.reset();    // 0000000000
    b.set(2);     // 0000000100
    b[3] = 1;     // 0000001100
    b[3];         // 1
    b[1];         // 0
    b.size();     // 10

    cout << "\"1101001001\" as number: "
         << bitset<64>(string("1101001001"))
                    .to_ulong() << endl;
}
\end{lstlisting}

%%% Local Variables:
%%% mode: latex
%%% TeX-master: "ref-doc"
%%% End:
