Bitset Set Function - C++ Programming Tutorial
C++ Course / STL Set & Map / Bitset Set Function

Bitset Set Function

BLUF: Mastering Bitset Set Function is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: Bitset Set Function

C++ is renowned for its efficiency. Learn how Bitset Set Function enables low-level control and high-performance computing in the tutorial below.

The set function in C++ bitset is employed to assign a value of 1 to all the bits. When a single parameter is provided, it updates the bit at the specified index to 1.

Syntax

Example

set(int index, bool Val);
set();

Parameter

The index parameter indicates the specific location where the bit needs to be set. This parameter is not required and can be omitted if not needed.

The 'val' parameter indicates a boolean value that needs to be assigned to the specified index. It is an optional parameter.

Return value

It does not return any value.

Example 1

Example

#include <iostream>
#include <bitset>
using namespace std;
int main()
{
bitset<4> b(string("1001"));
cout<< "before applying set method : " << b <<'\n';
cout<< "after applying reset method : " <<b.set() <<'\n';
return 0;
}

Output:

Output

before applying set method : 1001
after applying reset method : 1111

Example 2

Example

#include <iostream>
#include <bitset>
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:

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

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience