//complie with g++
#include <iostream>
#include <bitset>
using namespace std;
int main()
{
const int count=50;
bitset<count> bits;
uint32_t mask;
bits = 0;
mask = 0;
cout << "All off" << endl;
cout << bits << endl;
cout << hex << mask << endl;
//setting
for(int i=0; i<count; i+=3)
{
bits[i] = true;
mask |= 1<<i;
}
cout << "Every 3rd on" << endl;
cout << bits << endl;
cout << hex << mask << endl;
//masking test
cout << "Self invert with ^" << endl;
cout << (bits ^ bits) << endl;
cout << hex << (mask ^ mask) << endl;
cout << "Self mask with &" << endl;
cout << (bits & bits) << endl;
cout << hex << (mask & mask) << endl;
//turn on all
bits = 0; bits.flip();
mask = -1;
cout << "All on" << endl;
cout << bits << endl;
cout << hex << mask << endl;
bits = -1; bits.flip();
cout << "when bits are: " << bits << endl;
cout << "check any on: " << (bits != 0) << endl;
cout << "check any off: " << (~bits != 0) << endl;
bits = 0;
cout << "when bits are: " << bits << endl;
cout << "check any on: " << (bits != 0) << endl;
cout << "check any off: " << (~bits != 0) << endl;
bits.flip();
cout << "when bits are: " << bits << endl;
cout << "check any on: " << (bits != 0) << endl;
cout << "check any off: " << (~bits != 0) << endl;
return 0;
}This outputs the following
All off 00000000000000000000000000000000000000000000000000 0 Every 3rd on 01001001001001001001001001001001001001001001001001 4925b6db Self invert with ^ 00000000000000000000000000000000000000000000000000 0 Self mask with & 01001001001001001001001001001001001001001001001001 4925b6db All on 11111111111111111111111111111111111111111111111111 ffffffff when bits are: 11111111111111111100000000000000000000000000000000 check any on: 1 check any off: 1 when bits are: 00000000000000000000000000000000000000000000000000 check any on: 0 check any off: 1 when bits are: 11111111111111111111111111111111111111111111111111 check any on: 1 check any off: 0
No comments:
Post a Comment