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:
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: 111111Program 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
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4