Online Book Reader

Home Category

Professional C__ - Marc Gregoire [219]

By Root 1445 0
is:

Bit 3 is set!

1101001000

Note that the leftmost character in the output string is the highest numbered bit. This corresponds to our intuitions about binary number representations, where the low-order bit representing 20 = 1 is the rightmost bit in the printed representation.

Bitwise Operators

In addition to the basic bit manipulation routines, the bitset provides implementations of all the bitwise operators: &, |, ^, ~, <<, >>, &=, |=, ^=, <<=, and >>=. They behave just as they would on a “real” sequence of bits. Here is an example:

auto str1 = "0011001100";

auto str2 = "0000111100";

bitset<10> bitsOne(str1);

bitset<10> bitsTwo(str2);

auto bitsThree = bitsOne & bitsTwo;

cout << bitsThree << endl;

bitsThree <<= 4;

cout << bitsThree << endl;

Code snippet from BitsetBasics\BitwiseOperators.cpp

The output of the program is:

0000001100

0011000000

bitset Example: Representing Cable Channels

One possible use of bitsets is tracking channels of cable subscribers. Each subscriber could have a bitset of channels associated with his or her subscription, with set bits representing the channels to which he or she actually subscribes. This system could also support “packages” of channels, also represented as bitsets, which represent commonly subscribed combinations of channels.

The following CableCompany class is a simple example of this model. It uses two maps, each of string/bitset, storing the cable packages as well as the subscriber information.

using std::map;

using std::bitset;

using std::string;

using std::out_of_range;

const int kNumChannels = 10;

class CableCompany

{

public:

CableCompany() {}

// Adds the package with the specified channels to the database

void addPackage(const string& packageName,

const bitset& channels);

// Removes the specified package from the database

void removePackage(const string& packageName);

// Adds customer to database with initial channels found in package

// Throws out_of_range if the package name is invalid.

void newCustomer(const string& name, const string& package)

throw(out_of_range);

// Adds customer to database with initial channels specified

// in channels

void newCustomer(const string& name,

const bitset& channels);

// Adds the channel to the customers profile

void addChannel(const string& name, int channel);

// Removes the channel from the customers profile

void removeChannel(const string& name, int channel);

// Adds the specified package to the customers profile

void addPackageToCustomer(const string& name,

const string& package);

// Removes the specified customer from the database

void deleteCustomer(const string& name);

// Retrieves the channels to which this customer subscribes

// Throws out_of_range if name is not a valid customer

bitset& getCustomerChannels(const string& name)

throw(out_of_range);

protected:

typedef map > MapType;

MapType mPackages, mCustomers;

};

Code snippet from CableCompany\CableCompany.h

Here are the implementations of the preceding methods, with comments explaining the code:

void CableCompany::addPackage(const string& packageName,

const bitset& channels)

{

// Just make a key/value pair and insert it into the packages map.

mPackages.insert({packageName, channels});

}

void CableCompany::removePackage(const string& packageName)

{

// Just erase the package from the package map.

mPackages.erase(packageName);

}

void CableCompany::newCustomer(const string& name, const string& package)

throw(out_of_range)

{

// Get a reference to the specified package.

auto it = mPackages.find(package);

if (it == mPackages.end()) {

// That package doesn't exist. Throw an exception.

throw out_of_range("Invalid package");

} else {

// Create the account with the bitset representing that package.

// Note that 'it' refers to a name/bitset pair. The bitset is the

// second field.

mCustomers.insert({name, it->second});

}

}

void CableCompany::newCustomer(const string& name,

const bitset& channels)

Return Main Page Previous Page Next Page

®Online Book Reader