Skip to content
Related Articles

Related Articles

Implementation of C++ Bitset using String

View Discussion
Improve Article
Save Article
  • Difficulty Level : Hard
  • Last Updated : 28 Mar, 2022
View Discussion
Improve Article
Save Article

Let’s implement bitset in C++, such that following operations can be performed in stated time complexities :

  • init(int size): initializes a bitset of size number of 0 bits.
  • void fix(int pos): Change the bit at position pos to 1. No change if it was already 1.
  • void unfix(int pos):  Change the bit at position pos to 0. No change if it was already 0.
  • void flip(): Change all 1s to 0s and all 0s to 1s.
  • bool check() : If all values of bitset are 1 , return true , else return false.
  • bool checkOne() : If at least one bit is 1 , return true. Else return false.
  • int countOne() : return number of bits whose value is 1.
  • string Value(): returns the current value of bitset in form of a string.

The init(size) must have O(size) complexity (approximately). The rest of the operations must have constant complexity.

O(size/d) where d can be 32 or 64(depending upon bits).

Implementation: The required implementation can be done easily by maintaining a string of a given size and performing all operations on it by simultaneously keeping a count of 0s and 1s in it. But the flip operation would not be possible in constant time with this approach. 

So, instead of 1, we will maintain two strings. One is the original string and the other is the opposite string of the original string (i.e. at the positions of 0s, we will keep 1s and vice-versa). In flip operation, we can simply swap the 2 strings.

Following is the implementation for the above approach :

C++




// C++ code for the above approach
#include <bits/stdc++.h>
using namespace std;
  
// initializing variables to store
// the number of 0s , 1s and size of
// out bitset
int zero = 0, one = 0, sz = 0;
  
// string "s" is the string to store
// our bitset, string "t" is the
// helper string to make flip()
// function complexity O(1)
string s, t;
  
// function to initialize the bitset
// of size "size"
void init(int size)
{
    string ss(size, '0');
    string tt(size, '1');
    t = tt;
    s = ss;
  
    // initially number of zeroes in
    // string s is equal to size
    zero = size;
    sz = size;
}
  
// function to make bit at position
//"pos" equal to 1
void fix(int pos)
{
    // setting bit at position "pos"
    // equal to 1 in string s and equal
    // to 0 in string t and simultaneously
    // updating
    // number of 0s and 1s in string s
    if (s[pos] == '0') {
        one++;
        zero--;
    }
    t[pos] = '0';
    s[pos] = '1';
}
  
// function to make bit at position "pos"
// equal to 0
void unfix(int pos)
{
    // setting bit at position "pos" equal
    // to 0 in string s and equal to 1 in
    // string t and simultaneously
    // updating
    // number of 0s and 1s in string s
    if (s[pos] == '1') {
        zero++;
        one--;
    }
    t[pos] = '1';
    s[pos] = '0';
}
  
// function to flip the bits of bitset
void flip()
{
    // for flip operation , we will simply
    // swap the strings s and t as they are
    // just opposite of each other. Also,
    // we will swap number of 0s and 1s in
    // string s obviously
    swap(s, t);
    swap(one, zero);
}
  
// function to check if all the elements of
// bitset are '1'
bool check()
{
    // If number of 1s in string s is equal
    // to the size of s, that means all the
    // characters of s are 1.
    return (one == sz);
}
  
// function for checking if there is atleast
// one '1' in bitset
bool checkOne() { return (one > 0); }
  
// function for returning the number of 1s
// in bitset
int countOne() { return one; }
  
// function for returning value of string s
// or the bitset
string Value() { return s; }
  
int main()
{
    init(5); // 00000
    fix(1); // 01000
    fix(2); // 01100
    unfix(1); // 00100
    flip(); // 11011
    int number_of_ones = countOne(); // 4
    string bitset = Value(); // 11011
    cout << "number of ones in our bitset are: "
         << number_of_ones << endl;
    cout << "Value of our bitset is: " << bitset << endl;
}


Output

number of ones in our bitset are: 4
Value of our bitset is: 11011

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!