Need help to explain each step #include #include using namespace std; class BITSET{ private : vector < int > bits; void increaseSet( int n){ long
Need help to explain each step
#include
#include
using namespace std;
class BITSET{
private:
vector<int> bits;
void increaseSet(int n){
long unsigned int s = bits.size(); // modified
bits.resize(s+(32*n));
for(long unsigned int i=s;i bits[i-1] = 0; } } bool shrink(){ long unsigned int n = bits.size(); // modified if(n <= 32){ return false; } bool shr = true; for(long unsigned int i = n-32;i if(bits[i] == 1){ shr = false; break; } } if(shr){ bits.resize(n-32); } return shr; } public: BITSET(){ increaseSet(1); } bool Test(long unsigned int bit){ // modified if(bit < 0 && bit >= bits.size()){ return false; } unsigned long n = bit/32; unsigned long p = bit%32; unsigned long pos = (n*32) + (31-p); return bits[pos] == 1; } void Set(long unsigned int bit){ // modified while(bit > bits.size()){ increaseSet(1); } unsigned long n = bit/32; unsigned long p = bit%32; unsigned long pos = (n*32) + (31-p); bits[pos] = 1; } void Clear(long unsigned int bit){ // modified if(bit < 0 && bit >= bits.size()){ return; } unsigned long n = bit/32; unsigned long p = bit%32; unsigned long pos = (n*32) + (31-p); bits[pos] = 0; while(shrink()); } unsigned long GetNumSets(){ return bits.size()/32; } void inc(){ increaseSet(1); } void print(int n){ if(n < 0 || n > GetNumSets()){ return; } for(int i = n*32;i<(n+1)*32;i++){ if(i%32 == 0){ printf(" SET %d: ",i/32); } if(i%4 == 0){ printf(" "); } printf("%d",bits[i]); } } }; string ToBinary(int val,int space){ BITSET b; int i = 0; while(val > 2){ if(val%2 == 0){ b.Clear(i); }else{ b.Set(i); } i++; val = val/2; } if(val == 1){ b.Set(i); } if(val == 2){ b.Clear(i); i++; b.Set(i); } string s = ""; for(int i = 32;i>0;i--){ if(i != 32 && i % space == 0){ s += ' '; } if(b.Test(i)){ s += '1'; }else{ s += '0'; } } cout< return s; } int main(){ BITSET b; char command = ' '; while(command != 'q'){ printf(" CMD >> "); scanf("%c",&command); switch(command){ case 't': { int n; scanf("%d",&n); if(b.Test(n)){ printf(" 1"); }else{ printf(" 0"); } break; } case 's': { int n; scanf("%d",&n); b.Set(n); break; } case 'g': { int n; scanf("%d",&n); b.print(n); break; } case 'n': { int n = b.GetNumSets(); printf(" Number Of Set : %d",n); break; } case 'q': { command = 'q'; break; } case 'c': { int n; scanf("%d",&n); b.Clear(n); break; } default: printf(" INVALID COMMAND !!! "); } char c; scanf("%c",&c); // modified to handle the buffer, if it should not be done, please comment you will get what you want } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started