This C++ program illustrates the usage of bitset. The bitset stores bits in the form 1s and 0s which emulates true and false. This class is optimized for space allocation and each of element of it occupies only one bit. The program uses several common bitset operations and functions.
Here is the source code of the C++ program which illustrates the usage of bitset. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.
/*
* C++ Program to Illustrate usage of bitset
*/
#include <iostream>
#include <bitset>
int main()
{
std::bitset <5> a, b;
b.set();
for (int i = 0; i < 5 ;i++)
{
if (i % 2 == 0)
a[i].flip();
}
std::cout << "Bitset a: " << a << std::endl;
std::cout << "Bitset b: " << b << std::endl;
std::cout << std::endl;
std::cout << "a XOR b = " << (a ^ b) << std::endl;
std::cout << "a AND b = " << (a & b) << std::endl;
std::cout << "a OR b = " << (a | b) << std::endl;
std::cout << " NOT(a) = " << (~a) << std::endl;
}
$ a.out Bitset a: 10101 Bitset b: 11111 a XOR b = 01010 a AND b = 10101 a OR b = 11111 NOT(a) = 01010
Sanfoundry Global Education & Learning Series – 1000 C++ Programs.
advertisement
If you wish to look at all C++ Programming examples, go to C++ Programs.
Related Posts:
- Apply for Computer Science Internship
- Practice Programming MCQs
- Apply for C++ Internship
- Practice Computer Science MCQs
- Check Computer Science Books