Skip to content
Related Articles

Related Articles

bitset set() function in C++ STL
  • Difficulty Level : Easy
  • Last Updated : 18 Jun, 2018

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.

    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:




// 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;
}
Output:
Before applying set() function: 1100
After applying set() function: 1111
Before applying set() function: 100100
After applying set() function: 111111

Program 2:




// 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;
}
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
Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.
My Personal Notes arrow_drop_up
Recommended Articles
Page :