C++ Standard Library C++ STL Library

C++ <bitset> - reset() Function



The C++ bitset::reset function is used to reset (to zero) bit at specified position of the bitset. Along with this, it is also used to reset (to zero) all bits of the bitset.

Syntax

//reset all bits
bitset& reset();

//reset single bit	
bitset& reset (size_t pos);
//reset all bits
bitset& reset() noexcept;

//reset single bit	
bitset& reset (size_t pos);

Parameters

pos Specify position of the bit whose value is modified.

Return Value

*this

Exception

Version 1: never throws exception. Version 2: throws out_of_range, if specified position is not a valid bit position.

Example:

The example below shows the usage of bitset::reset function.

#include <iostream>
#include <bitset>
using namespace std;
 
int main (){
  bitset<4> bset("1011");
  
  cout<<"1. bset is: "<<bset<<"\n";

  //reset bit at specified position
  bset.reset(1);
  cout<<"2. bset is: "<<bset<<"\n";

  //reset all bits
  bset.reset();
  cout<<"3. bset is: "<<bset<<"\n";

  return 0;
}

The output of the above code will be:

1. bset is: 1011
2. bset is: 1001
3. bset is: 0000

❮ C++ <bitset> Library