What is the bitset::reset() function in C++?

The bitset::reset() function in C++ is used to reset the bits to zero. There are two functionalities that can be performed using this function:

  • Reset all the bits to zero.

  • Reset the bit as zero at a specified position.

This function is available in the bitset header file.

Syntax

The syntax of bitset::reset() function is shown below.

bitset& reset();
bitset& reset(size_t pos);

Parameters

The function accepts only an optional parameter as shown below.

  • pos: This parameter is an unsigned integer and specifies the position of the bit that needs to be resetconverted to zero.

Return value

The function returns the current bitset object that contains the binary representation after applying the reset() function.

Code

Let’s have a look at the code now.

#include <iostream>
#include <bitset>
using namespace std;
int main() {
bitset<4> b1 = 15;
cout << "Original value: " << b1 << endl;
cout << "Flipped second bit : " << b1.reset(2) << endl;
cout << "Flipped all bits: " << b1.reset();
return 0;
}

Explanation

  • In lines 1 and 2, we import the required header files.

  • In line 6, we create one bitset object that has the capacity to store 4 bits. This object will contain the binary representation of the integer 10.

  • In line 7, we print the original bit representation of the integer 10.

  • In line 8, we call the reset() function, and we pass the argument as 2 to reset the second bit. Then we print the new binary representation.

  • In line 9, we call the reset() function and print the binary representation that contains all the bits to be reset to 0.

In this way, we can reset the bits to zero or any number using the reset() function in C++.