bitset set() function in C++ STL Last Updated : 18 Jun, 2018 Comments Improve Suggest changes Like Article Like Report 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: index - this parameter specifies the position at which the bit has to be set. The parameter is an optional one. 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 // 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 // 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 Comment More infoAdvertise with us Next Article bitset set() function in C++ STL G gopaldave Follow Improve Article Tags : Misc C++ STL CPP-Functions CPP-bitset +1 More Practice Tags : CPPMiscSTL Similar Reads bitset reset() function in C++ STL bitset::reset() is a built-in function in C++ STL which resets the bits at the given index in the parameter. If no parameter is passed then all the bits are reset to zero. Syntax: reset(int index) Parameter: The function accepts a parameter index which signifies the position at which the bit has to 2 min read bitset all() function in C++ STL The bitset::all() is a built-in function in C++ STL which returns True if all bits are set in the binary representation of a number if it is initialized in the bitset. It returns False if all the bits are not set. Syntax: bool bitset_name.all() Parameter: This function does not accepts any parameter 2 min read set::count() Function in C++ STL The std::set::count() is a built-in function in C++ STL which is used to count the number of times an element occurs in the set container. std::set container stores unique elements, so it can only return 1 or 0. Therefore, it is only used for checking if the element exists in the set or not.ExampleC 3 min read set find() Function in C++ STL The std::set::find() is a built-in function in C++ STL that is used to find an element in the set container. It is a member function of std::set container so we can use it directly with any set object.Syntax set_name.find(key) Parameterskey: The element which we have to find.Return ValueIf the eleme 2 min read Counting Set Bits in C++ Counting set bits means determining how many bits in a binary representation of a number are set to 1.ExampleInput: n = 103 (01100111)Output: 5Explanation: The binary representation of 103 is 01100111. Hence, the number of set bits is 5.Input: n = 15 (1111)Output: 4Explanation: The binary representa 4 min read Like