bitset set() function in C++ STL

bitset::set() is a built-in STL in C++ which sets the bit to a given value at a particular index. If no parameter is passed, it sets all bits to 1. If only a single parameter is passed, it sets the bit at that particular index to 1.

Syntax:

set(int index, bool val) 

Parameter: The function accepts two parameters which are described below:

  1. index – this parameter specifies the position at which the bit has to be set. The parameter is an optional one.
  2. val – this parameter specifies a boolean value which has to bet set at the index. The parameter is an optional one.
  3. If no parameter is passed, it sets all bits to 1. If only a single parameter is passed, it sets the bit at that index.

Return Value: The function does not return anything.

Below programs illustrates the bitset::set() function.



Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to illustrate the
// bitset::set() function
// when parameter is not passed
  
#include <bits/stdc++.h>
using namespace std;
  
int main()
{
    // Initialization of bitset
    bitset<4> b1(string("1100"));
    bitset<6> b2(string("100100"));
  
    // Function that resets all bits
    cout << "Before applying set() function: "
         << b1 << endl;
  
    b1.set();
    cout << "After applying set() function: "
         << b1 << endl;
  
    // Function that resets all bits
    cout << "Before applying set() function: "
         << b2 << endl;
  
    b2.set();
    cout << "After applying set() function: "
         << b2 << endl;
  
    return 0;
}

chevron_right


Output:

Before applying set() function: 1100
After applying set() function: 1111
Before applying set() function: 100100
After applying set() function: 111111

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to illustrate the
// bitset::set() function
// when parameter is passed
  
#include <bits/stdc++.h>
using namespace std;
  
int main()
{
    // Initialization of bitset
    bitset<4> b1(string("1100"));
    bitset<6> b2(string("100100"));
  
    // Function that resets all bits
    cout << "Before applying set() function: "
         << b1 << endl;
  
    // single parameter is passed
    b1.set(1);
    cout << "After applying set(1) function: "
         << b1 << endl;
  
    // Function that resets all bits
    cout << "Before applying set() function: "
         << b2 << endl;
  
    // both parameters is passed
    b2.set(2, 0);
    b2.set(4, 1);
    cout << "After applying set(2, 0) and"
         << " set(4, 1) function: " << b2 << endl;
  
    return 0;
}

chevron_right


Output:

Before applying set() function: 1100
After applying set(1) function: 1110
Before applying set() function: 100100
After applying set(2, 0) and set(4, 1) function: 110000


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.