I was trying to understand Bitmasking and came across this problem where i have to store number upto 64 (1<n<64) in O(1) complexity : Here I'm trying to make a Data Structure to store values as a set but in O(1) as I have constraints of 64
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
class BitMask
{
public:
ll n;
BitMask()
{
//call the class as Bitmask varname;
n = 0;
}
void insert(long long x) {
n |= (1 << x);
}
void deleteItem(long long x) {
if (n & (1 << x)) {
n &= ~(1 << x);
}
}
void print() {
for (int i = 0; i < 64; i++) {
if (n & (1 << i)) {
cout << i << endl;
}
}
}
};
//implementation
int main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
BitMask obj1;
//insertion
obj1.insert(1);
obj1.insert(5);
obj1.insert(3);
obj1.insert(60);
//print
obj1.print();
cout << endl;
//deletion
obj1.deleteItem(5);
obj1.print();
cout << endl;
return 0;
}
Here The output was supposed to give
1, 3, 5, 60
1,3,60
But instead this is giving
1 3 5 28 33 35 37 60
1 3 28 33 35 60
Can some tell why these extra bits are called out?