A Bitset is a data structure that compactly stores bits.

Implement the Bitset class:


	Bitset(int size) Initializes the Bitset with size bits, all of which are 0.
	void fix(int idx) Updates the value of the bit at the index idx to 1. If the value was already 1, no change occurs.
	void unfix(int idx) Updates the value of the bit at the index idx to 0. If the value was already 0, no change occurs.
	void flip() Flips the values of each bit in the Bitset. In other words, all bits with value 0 will now have value 1 and vice versa.
	boolean all() Checks if the value of each bit in the Bitset is 1. Returns true if it satisfies the condition, false otherwise.
	boolean one() Checks if there is at least one bit in the Bitset with value 1. Returns true if it satisfies the condition, false otherwise.
	int count() Returns the total number of bits in the Bitset which have value 1.
	String toString() Returns the current composition of the Bitset. Note that in the resultant string, the character at the ith index should coincide with the value at the ith bit of the Bitset.


&nbsp;
Example 1:

Input
["Bitset", "fix", "fix", "flip", "all", "unfix", "flip", "one", "unfix", "count", "toString"]
[[5], [3], [1], [], [], [0], [], [], [0], [], []]
Output
[null, null, null, null, false, null, null, true, null, 2, "01010"]

Explanation
Bitset bs = new Bitset(5); // bitset = "00000".
bs.fix(3);     // the value at idx = 3 is updated to 1, so bitset = "00010".
bs.fix(1);     // the value at idx = 1 is updated to 1, so bitset = "01010". 
bs.flip();     // the value of each bit is flipped, so bitset = "10101". 
bs.all();      // return False, as not all values of the bitset are 1.
bs.unfix(0);   // the value at idx = 0 is updated to 0, so bitset = "00101".
bs.flip();     // the value of each bit is flipped, so bitset = "11010". 
bs.one();      // return True, as there is at least 1 index with value 1.
bs.unfix(0);   // the value at idx = 0 is updated to 0, so bitset = "01010".
bs.count();    // return 2, as there are 2 bits with value 1.
bs.toString(); // return "01010", which is the composition of bitset.


&nbsp;
Constraints:


	1 &lt;= size &lt;= 105
	0 &lt;= idx &lt;= size - 1
	At most 105 calls will be made in total to fix, unfix, flip, all, one, count, and toString.
	At least one call will be made to all, one, count, or toString.
	At most 5 calls will be made to toString.


class Bitset(object):

    def __init__(self, size):
        self.a = 0
        self.size = size
        self.cnt = 0

    def fix(self, idx):
        if self.a & (1 << idx) == 0:
            self.a |= 1 << idx
            self.cnt += 1

    def unfix(self, idx):
        if self.a & (1 << idx):
            self.a ^= 1 << idx
            self.cnt -= 1

    def flip(self):
        self.a ^= (1 << self.size) - 1
        self.cnt = self.size - self.cnt

    def all(self):
        return self.cnt == self.size

    def one(self):
        return self.a > 0

    def count(self):
        return self.cnt

    def toString(self):
        a = bin(self.a)[2:]
        return a[::-1] + '0' * (self.size - len(a))
class Bitset {
    int size;
    Set<Integer> one = new HashSet<>();
    Set<Integer> zero = new HashSet<>();
    public Bitset(int size) {
        this.size = size;
        for(int i=0;i<size;i++) zero.add(i);
    }
    
    public void fix(int idx) {
        one.add(idx);
        zero.remove(idx);
    }
    
    public void unfix(int idx) {
        one.remove(idx);
        zero.add(idx);
    }
    
	//swapping object's referrence is O(1)
    public void flip() {
        Set<Integer> s = one;
        one = zero;
        zero = s;
    }
    
    public boolean all() {
        return one.size() == size;
    }
    
    public boolean one() {
        return one.size()>=1;
    }
    
    public int count() {
        return one.size();
    }
    
    public String toString() {
        StringBuilder sb=  new StringBuilder();
        for(int i=0;i<size;i++) {
            if(one.contains(i)) sb.append("1"); 
            else if(zero.contains(i)) sb.append("0");
        }
        return sb.toString();
    }
}
class Bitset {
public:
    vector<int>arr;
    int cnt,cntflip;
    Bitset(int size) {
        arr.resize(size,0);
        cnt=0,cntflip=0;
    }
    void fix(int idx) {
	// means current bit is 0 ,so set it to 1
        if((arr[idx]+cntflip)%2==0){
            arr[idx]++;
            cnt++;
        }
    }
    void unfix(int idx) {
	// means current bit is 1,so set it to 0
      if((arr[idx]+cntflip)%2!=0){
            arr[idx]--;
            cnt--;
        }  
    }
    void flip() {
	// cnt will flip ,if we flip all the bits
         cnt=arr.size()-cnt;
        cntflip++;
    }
    bool all() {
        if(cnt==arr.size())
            return true;
        return false;
    }
    bool one() {
        if(cnt>=1)
            return true;
        return false;
    }
    int count() {
        return cnt;
    }
    string toString() {
        string ans;
        for(auto &ele :arr){
            if((cntflip+ele)%2==0)
                ans.push_back('0');
            else
                ans.push_back('1');  
        }
        return ans;
    }
};
var Bitset = function(size) {
    //this.bits = new Array(size).fill(0);
    //this.bitsOp = new Array(size).fill(1);
    this.set0 = new Set();
    this.set1 = new Set();
    for (let i = 0; i<size; i++) this.set0.add(i);
};

/** 
 * @param {number} idx
 * @return {void}
 */
Bitset.prototype.fix = function(idx) {
  //this.bits[idx] = 1;
  //this.bitsOp[idx] = 0;
  if (this.set0.has(idx)) {
    this.set1.add(idx);
    this.set0.delete(idx);
  }
};

/** 
 * @param {number} idx
 * @return {void}
 */
Bitset.prototype.unfix = function(idx) {
  //this.bits[idx] = 0;
  //this.bitsOp[idx] = 1;
  if (this.set1.has(idx)) {
    this.set0.add(idx);
    this.set1.delete(idx);
  }
};

/**
 * @return {void}
 */
Bitset.prototype.flip = function() {
  //this.bits = this.bits.map(x=>(x)?0:1); 
  //this.bits.forEach(x=>x=(x)?0:1); 
  [this.set0, this.set1] = [this.set1, this.set0];
};

/**
 * @return {boolean}
 */
Bitset.prototype.all = function() {
  //return (this.bits.filter(x=>x).length == this.bits.length) 
  //return !this.bits.includes(0);
  //for (let i = 0; i<this.bits.length; i++)
    //if (this.bits[i] === 0) return false;
   //return true; 
  return this.set0.size===0
};

/**
 * @return {boolean}
 */
Bitset.prototype.one = function() {
    //return this.bits.filter(x=>x).length>0 
    //return this.bits.includes(1);
    //for (let i = 0; i<this.bits.length; i++)
      //if (this.bits[i] === 1) return true;
     //return false; 
  return this.set1.size>0;
};

/**
 * @return {number}
 */
Bitset.prototype.count = function() {
    //return this.bits.filter(x=>x).length 
  //return this.bits.reduce((sum, cur)=>sum+cur);
  return this.set1.size;
};

/**
 * @return {string}
 */
Bitset.prototype.toString = function() {
    //return this.bits.join('');
  let set = new Array(this.set0.size+this.set1.size);
  for (let i=0; i<set.length; i++) {
    set[i] = this.set0.has(i)?0:1;
  }
  return set.join('');
};
Design Bitset