bitset size() in C++ STL


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to illustrate the
// bitset::flip() function
// when input is a string
  
#include <bits/stdc++.h>
using namespace std;
  
int main()
{
    // initilization of the bitset string
    bitset<4> b1(string("1100"));
    bitset<6> b2(string("010010"));
  
    // prints the size i.e.,
    // the number of bits in "1100"
    cout << "The number of bits in " << b1
         << " is " << b1.size() << endl;
  
    // prints the size i.e.,
    // the number of bits in  "010010"
    cout << "The number of bits in " << b2
         << " is " << b2.size() << endl;
  
    return 0;
}

chevron_right


Output:

The number of bits in 1100 is 4
The number of bits in 010010 is 6

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to illustrate the
// bitset::flip() function
// when input is a number
  
#include <bits/stdc++.h>
using namespace std;
  
int main()
{
    // initilization of the bitset string
    bitset<3> b1(5);
    bitset<5> b2(17);
  
    // prints the size i.e.,
    // the number of bits in 5
    cout << "The number of bits in " << b1
         << " is " << b1.size() << endl;
  
    // prints the size i.e.,
    // the number of bits in 17
    cout << "The number of bits in "
         << b2 << " is " << b2.size() << endl;
  
    return 0;
}

chevron_right


Output:

The number of bits in 101 is 3
The number of bits in 10001 is 5


My Personal Notes arrow_drop_up

The function of education is to teach one to think intensively and to think critically

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.