I'm going to agree with Oli, and say, if you want to do "big-integer" stuff, then you should use big integer library.
However, if you really want to do this using std::bitset
, you'll need to do the arithmetic yourself.
template <size_t N>
std::bitset<N> increment ( std::bitset<N> in ) {
// add 1 to each value, and if it was 1 already, carry the 1 to the next.
for ( size_t i = 0; i < N; ++i ) {
if ( in[i] == 0 ) { // There will be no carry
in[i] = 1;
break;
}
in[i] = 0; // This entry was 1; set to zero and carry the 1
}
return in;
}
int main () {
std::bitset<32> foo;
std::cout << foo.to_ulong () << ' ';
foo = increment ( foo );
std::cout << foo.to_ulong () << ' ';
foo = increment ( foo );
std::cout << foo.to_ulong () << ' ';
foo = increment ( foo );
std::cout << foo.to_ulong () << std::endl;
return 0;
}
This prints 0 1 2 3
for me.