C++ Bitset Library - set() Function



Description

The C++ function std::bitset::set() set single bit from bitset to either one or zero.

Declaration

Following is the declaration for std::bitset::set() function form std::bitset header.

C++98

bitset& set (size_t pos, bool val = true);

C++11

bitset& set (size_t pos, bool val = true);

Parameters

  • pos − Position of the bit whose value is flipped.

  • val − Value to be set.

Return value

Returns this pointer.

Exceptions

Throws out_of_range exception if pos is greater than or equal to bitset size.

Example

The following example shows the usage of std::bitset::set() function.

#include <iostream>
#include <bitset>

using namespace std;

int main(void) {

   bitset<4> b;

   cout << "Before set operation b = " << b << endl;

   b.set(0, 1);

   cout << "After set operation b = " << b << endl;

   return 0;
}

Let us compile and run the above program, this will produce the following result −

Before set operation b = 0000
After set operation b = 0001
bitset.htm
Advertisements