A programmer is a person who passes as an exacting expert on the basis of being able to turn out, after innumerable punching, an infinite series of incomprehensive answers calculated with micrometric precisions from vague assumptions based on debatable figures taken from inconclusive documents and carried out on instruments of problematical accuracy by persons of dubious reliability and questionable mentality for the avowed purpose of annoying and confounding a hopelessly defenseless department that was unfortunate enough to ask for the information in the first place. Grid newsmagazine

bitset

Language C++ | Level Intermediate | Category Standard Template Library (STL) | 2015-11-16 22:01:21

0 Comments


Definition

The bit set is a container for storing the data at the bit level.

Character

Bit 0 is the least significant bit and bit N-1 is the most significant bit.
Provides serialize / de-serialize with ostream and fstream.
The size of a bit set is determined at compile time.
Conversion mechanism to and from a textual representation.
A bit is set if its value is 1 and reset if its value is 0.

source Code

								
#include "stdafx.h"

#include < bitset >
#include < iostream >

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	bitset<6> bit1(string("011101"));
	bitset<6> bit2(string("001111"));

	bitset<6> bitres = bit1 | bit2;
	bitset<6> bitresand = bit1 & bit2;

	cout << "Bitset one " << bit1 << endl;
	cout << "Bitset two " << bit2 << endl;
	cout << "OR with two bitset: " << bitres << endl;
	cout << "AND with two bitset: " << bitresand << endl;

	return 0;
}

						

Output

    					 
					Bitset one 011101
					Bitset two 001111
					OR with two bitset: 011111
					AND with two bitset: 001101
					    					 
					    

When to use

When you need to store space efficient, you can use bit set.

0 Comments



Please login to add comments.