Bitset class of c++.
It will has methods that will perform operations on the bits of the numbers.
Following code has different operations along with its explanation in the comments section.
#include <stdio.h>
#include <iostream>
#include <conio.h>
#include <bitset>
using namespace std;
int main()
{
bitset<8> b; //MSB...LSB
cin>>b;
//This will return true if all the bits are set true
int i = b.all();
//This will return true if any of the bit is set to true
i = b.any();
//This will return true if the bit at position 1 is set to true
i = b.at(1);
//This will return the count of the bits that are set
i = b.count();
//It will flip the bit specified at the position 1
b.flip(4);
//It will return 1 will none of the bits are set. O/w zero
i = b.none();
//It will reset(set to zero) the specified bit
b.reset(1);
//It will set(to one) the specified bit
b.set(2);
//It will return the no. of bits specified in the bit vector
i = b.size();
//It will return 0 or 1. Whatever, is the value of the bit
i = b.test(2);
return 0;
}
More Concepts:
C++ Cheat Sheet
No comments:
Post a Comment