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:
- 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 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; } |
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; } |
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
Recommended Posts:
- bitset all() function in C++ STL
- bitset reset() function in C++ STL
- std::bitset::to_ullong and std::bitset::to_ulong in C++ STL
- bitset any() in C++ STL
- bitset none() in C++ STL
- bitset operator[] in C++ STL
- bitset test() in C++ STL
- bitset count() in C++ STL
- bitset::flip() in C++ STL
- C++ bitset and its application
- bitset size() in C++ STL
- C++ bitset interesting facts
- Subset sum queries using bitset
- Count number of set bits in a range using bitset
- Count number of common elements between two arrays by using Bitset and Bitwise operation
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.



