// Justin C. Miller
// made on : 11-29-2001
// made for: http://www.geocities.com/neonprimetime.geo/index.html
//
// TITLE: STL BITSET Tutorial
//
// STL bitset
// description: a bitset is just a collection of 1's and 0's
// or a set of bits that you can turn on and off
#include
#include
#include
using namespace std ;
const int size = 10 ;
int main(void){
cout << "Welcome to Justin Miller's" << endl ;
cout << "Quick Standard Template Library" << endl ;
cout << "Tutorial..." << endl ;
system("pause") ;
system("cls") ;
bitset b ;
cout << "bitset<10> b" << endl ;
cout << "You've created an empty bitset (all 0's) "<< endl ;
cout << "cout << b << endl -> " << b << endl ;
cout << "notice bitsets are overloaded to print out" << endl ;
cout << "a string of bits if you use cout << on them" << endl ;
system("pause") ;
system("cls") ;
b.set(0) ;
cout << "b.set(0) -> " << b << endl ;
cout << "Notice the right to left order..." << endl ;
cout << "Just as if you were reading a binary number" << endl << endl ;
system("pause") ;
system("cls") ;
b.set(3) ;
cout << "b.set(3) -> " << b << endl ;
cout << "b.set() sets a position to 1" << endl << endl ;
system("pause") ;
system("cls") ;
cout << "b.count() -> " << b.count() << endl ;
cout << "The count gives you the number of 1's " << endl << endl ;
system("pause") ;
system("cls") ;
cout << "size - b.count() -> " << (size - b.count()) << endl ;
cout << "The size - bitset.count() gives you the number of 0's" << endl << endl ;
system("pause") ;
system("cls") ;
cout << "b.at(3) =" << b.at(3) << ", b[3] =" << b[3] << ", b.test(3) =" << b.test(3) << endl ;
cout << "b.at(3), b[3], and b.test(3) are all ways to check " << endl;
cout << "what's at position 3" << endl << endl ;
system("pause") ;
system("cls") ;
cout << "b before -> " << b << endl ;
b.flip(3) ;
cout << "b.flip(3) -> " << b << endl ;
b.flip(8) ;
cout << "b.flip(8) -> " << b << endl ;
cout << "b.flip() flips a bit, 1 to 0 , or 0 to 1" << endl << endl ;
system("pause") ;
system("cls") ;
b.reset(8) ;
cout << "b.reset(8) -> " << b << endl ;
cout << "b.reset() sets a position to 0" << endl << endl ;
system("pause") ;
system("cls") ;
cout << "b.size() -> " << b.size() << endl ;
cout << "b.size() returns the size of the bitset" << endl << endl ;
system("pause") ;
system("cls") ;
cout << "b before -> " << b << endl ;
b[4] =true ;
b[5] =true ;
b[0] = false ;
cout << "b[0] = false , b[4] = true, b[5] = true " << endl ;
cout << "b now -> " << b << endl << endl ;
system("pause") ;
system("cls") ;
cout << "if(b == b) cout << '' they're equal '' << endl " << endl ;
if(b == b)
cout << " they're equal " << endl ;
cout << "OF COURSE, the == and != are overloaded for bitsets" << endl< " << b << endl ;
cout << "b >>= 3 -> " << (b >>= 3) << endl ;
cout << "shifts the whole bitset right 3" << endl ;
cout << "b <<= 2 -> " << (b <<= 2) << endl ;
cout << "shifts the whole bitset left 2" << endl;
cout << "Notice when shifting, anything that goes" << endl ;
cout << "Past the end of the bitset is lost, and can't be recovered" << endl << endl ;
system("pause") ;
system("cls") ;
bitset b2 ;
b2.set(3).set(2).set(5) ;
cout << "created a new bitset b2-> " << b2 << endl ;
cout << "and i set bits 3, 2, 5" << endl << endl ;
system("pause") ;
system("cls") ;
cout << "b -> " << b << endl ;
cout << "b2 -> " << b2 << endl ;
cout << "(b & b2) -> " << (b & b2) << endl ;
cout << "(b & b2) does a bitwise and on the 2 bitsets" << endl << endl ;
system("pause") ;
system("cls") ;
cout << "b -> " << b << endl ;
cout << "b2 -> " << b2 << endl ;
cout << "(b | b2) -> " << (b | b2) << endl ;
cout << "(b | b2) does a bitwise or on the 2 bitsets" << endl << endl ;
system("pause") ;
system("cls") ;
cout << "string mybitset = b.to_string()" << endl ;
string mybitset = b.to_string() ;
cout << "cout << mybitset << endl" << endl ;
cout << mybitset << endl ;
system("pause") ;
system("cls") ;
cout << "unsigned long myb = b.to_ulong()" << endl ;
unsigned long myb = b.to_ulong() ;
cout << "b -> " << b << endl ;
cout << "cout << myb << endl " << endl ;
cout << myb << endl ;
system("pause") ;
system("cls") ;
return 0 ;
}
               (
geocities.com/neonprimetime.geo/cpp)                   (
geocities.com/neonprimetime.geo)