bitset::size() is a built-in STL in C++ which returns the total number of bits.
Syntax:
bitset_name.size()
Parameter: The function accepts no parameter.
Return Value: The function returns an integral value which signifies the number of bits. It eventually returns the size that has been given while initializing the bitset.
Below programs illustrate the bitset::size() function.
Program 1:
C++
#include <bits/stdc++.h>
using namespace std;
int main()
{
bitset<4> b1(string( "1100" ));
bitset<6> b2(string( "010010" ));
cout << "The number of bits in " << b1
<< " is " << b1.size() << endl;
cout << "The number of bits in " << b2
<< " is " << b2.size() << endl;
return 0;
}
|
Output:
The number of bits in 1100 is 4
The number of bits in 010010 is 6
Program 2:
C++
#include <bits/stdc++.h>
using namespace std;
int main()
{
bitset<3> b1(5);
bitset<5> b2(17);
cout << "The number of bits in " << b1
<< " is " << b1.size() << endl;
cout << "The number of bits in "
<< b2 << " is " << b2.size() << endl;
return 0;
}
|
Output:
The number of bits in 101 is 3
The number of bits in 10001 is 5
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
16 Jun, 2021
Like Article
Save Article