_Find_first() function in C++ bitset with Examples

The _Find_first() is a built-in function in C++ Biteset class which returns an integer that refers the position of first set bit in bitset. If there isn’t any set bit, _Find_first() will return the size of the bitset.


iterator bitset._Find_first()
int bitset._Find_first()

Parameters: The function accepts no parameter.

Return Value: The function returns an integer which refers to the position of first set bit in bitset. If there isn’t any set bit, _Find_first() will return the size of the bitset.

Below is the illustration of the above function:





// C++ program for illustration
// of _Find_first() function
#include <bits/stdc++.h>
using namespace std;
#define M 32
int main()
    // default constructor initializes with all bits 0
    bitset<M> bset;
    bitset<M> bset1;
    // 00000000000000000000000000100000
    bset[5] = 1;
    // 00000000000000000000010000100000
    bset[10] = 1;
    // function returns the first set bit in Bitset
    cout << "position of first set bit in bset\n";
    cout << bset._Find_first() << "\n";
    // function returns bset1.size()
    // when no bit is set in bitset.
    cout << "position of first set bit in bset1\n";
    cout << bset1._Find_first() << "\n";
    return 0;



position of first set bit in bset
position of first set bit in bset1

Reference: https://gcc.gnu.org/onlinedocs/libstdc++/libstdc++-html-USERS-3.4/bitset-source.html

GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.