Wednesday, September 25, 2024

bitset

Overview
The standard library provides bitset to compactly store numbers as bits, access individual bit and perform logical operations on it.

Details
bitset is a template based compile time data structures that compactly stores numbers as 0 and 1 bits. Individual bits can be accessed by indexers that can be set or reset and logical operations such as and, or. xor etc can be performed.

Syntax
The syntax is as below. Template parameter represents number of bits.
template < size_t N > 
class bitset;

Members
 It defines following types
NameDescription
referenceproxy class to access individual bits

Operation
bitset can be graphically represented as below.
It's a template based, compile instantiation. Individual bits can be accessed using a subclass called  reference which is returned by the indexers Using this, individual bits can be flipped.

class bitset::reference 
{
  friend class bitset;
  reference() noexcept;                               // no public constructor
public:
  ~reference();
  operator bool() const noexcept;                     // convert to bool
  reference& operator= (bool x) noexcept;             // assign bool
  reference& operator= (const reference& x) noexcept; // assign bit
  reference& flip() noexcept;                         // flip bit value
  bool operator~() const noexcept;                    // return inverse value
}

Complexity
The complexity of access operation is O(1). Insertion or removal is O(N). 

Functionality
Constructors
NameDescription
bitset<size_t N>()Default Constructor.

Example:
//{0,0,0,0,0}
bitset<5> v;
bitset<size_t N>
(unsigned long long n)
Constructs bitset from a unsigned long long.

Example:
//{1,0,1,0,1}
bitset<5> v(21);
bitset<size_t N>
(const string& strsize_type n = npos,   
 char
zero = char('0'), char one = char('1'))
Constructs a bitset from a string or its substring.

Example:
//{1,0,1,0,1}
bitset<5> b(string("10101"));

//{1,0,1,0,0}
bitset<5> b2(string("10101"),2,3);

//{1,0,0,0,0}
bitset<5> b3(string("10101"),3,2);
bitset<size_t N>
(const char* str, size_type pos = 0,  
 size_type 
n = -1,  char zero = char('0'),
 char 
one = char('1'))
Constructs a bitset from a cstring or its substring.

Example:
//{1,0,1,0,1}
bitset<5> b("10101");

//{1,0,1,0,0}
bitset<5> b2("10101",2,3);

//{1,0,0,0,0}
bitset<5> b3("10101",3,2);

bit access and info
NameDescription
  1. bool operator[](size_type n)
  2. reference operator[](size_type n)
  1. Returns bool  to the indicate if the bit is 1 (true) or 0(false). No exception checks are made.
  2. Returns reference  to the bit at position n.
Example:
//{1,0,1,0,1}
bitset<5> b("10101");

//1
//false
bool bit2=b[1];

//2
//{1,1,1,0,1}
b[1].flip();
size_t count()Returns number of bits that are 1.
 
Example:
//{1,0,1,0,1}
bitset<5> b("10101");

//prints 3
cout << b.count();
size_t size()
Returns total number of bits.
 
Example:
//{1,0,1,0,1}
bitset<5> b("10101");

//prints 5
cout << b.size(); 
bool test
(size_t  pos)
Returns true if the bit at pos is set, otherwise false.  Exception is thrown for invalid input.

Example:
//{1,0,1,0,1}
bitset<5> b("10101");

//prints 1
cout << b.test(0);
bool any()
Returns true if any of the bit is set(1), otherwise false.  

Example:
//{1,0,1,0,1}
bitset<5> b("10101");

//prints 1
cout << b.any();
bool all()
Returns true if any of the bit is set(1), otherwise false.  

Example:
//{1,0,1,0,1}
bitset<5> b("10101");

//prints 0
cout << b.any();
bool none()
Returns true if none of the bits is set(1), otherwise false.  

Example:
//{1,0,1,0,1}
bitset<5> b("10101");

//prints 0
cout << b.any();

Modifiers
NameDescription
  1. bitset& set() 
  2. bitset& set
    (size_t pos, bool val = true)
  1. Sets all the bits and returns itself.
  2. Updates the bit at pos to val and returns itself.
Example:

//1
//b:{1,0,1,0,1}
bitset<5> b("10101");
//b:{1,1,1,1,1}
b.set();
    
//2
//b2:{1,0,1,0,1}
bitset<5> b2("10101");

//b2:{0,0,1,0,0}
b2.set(0,0);
    
//b2:{1,0,1,0,0}
b2.set(0);
  1. bitset& flip() 
  2. bitset& flip(size_t pos)
  1. Flips all the bits and returns itself.
  2. Flips the bit at pos and returns itself.
Example:

//1
//b:{1,0,1,0,1}
bitset<5> b("10101");
//b:{0,1,0,1,0}
b.flip();
    
//2
//b2:{1,0,1,0,1}
bitset<5> b2("10101");

//b2:{0,0,1,0,0}
b2.flip(0);

  1. bitset& reset() 
  2. bitset& reset (size_t pos)
  1. Resets all the bits and returns itself.
  2. reset the bit at pos and returns itself.
Example:

//1
//b:{1,0,1,0,1}
bitset<5> b("10101");
//b:{0,0,0,0,0}
b.set();
    
//2
//b2:{1,0,1,0,1}
bitset<5> b2("10101");

//b2:{0,0,1,0,0}
b2.set(0);

ulong to_ulong()Returns the ulong representation of the bitset. Throws Exceptions.

Example:
//b:{1,0,1,0,1}
bitset<5> b("10101");
//prints 21
cout << b.to_ulong() << endl;

bitset<100> b2;
//b2:11111111111111111111111111111111111111
11111111111111111111111111111111111111111111111
111111111111111
b2.set();
//throws exception
cout << b2.to_ulong() << endl;
string to_string()Returns the string  representation of the bitset. 

Example:
//b:{1,0,1,0,1}
bitset<5> b("10101");
//prints "10101"
cout << b.to_string() << endl;





No comments:

Post a Comment