Namespaces
Variants
Views
Actions

std::bitset::bitset

From cppreference.com
< cpp‎ | utility‎ | bitset
Revision as of 14:09, 10 April 2013 by Dieram3 (Talk | contribs)

Template:ddcl list begin <tr class="t-dcl ">

<td >
constexpr bitset() noexcept;
</td>

<td > (1) </td> <td class="t-dcl-nopad"> </td> </tr> <tr class="t-dcl ">

<td >
constexpr bitset( unsigned long long val ) noexcept;
</td>

<td > (2) </td> <td class="t-dcl-nopad"> </td> </tr> <tr class="t-dcl ">

<td >
template< class CharT, class Traits, class Allocator >

explicit bitset( const std::basic_string<CharT,Traits,Allocator>& str,
                 typename std::basic_string<CharT,Traits,Allocator>::size_type pos = 0,
                 typename std::basic_string<CharT,Traits,Allocator>::size_type n =
                     std::basic_string<CharT,Traits,Allocator>::npos);

         bitset( const std::basic_string<CharT,Traits,Allocator>& str,
                 typename std::basic_string<CharT,Traits,Allocator>::size_type pos = 0,
                 typename std::basic_string<CharT,Traits,Allocator>::size_type n =
                     std::basic_string<CharT,Traits,Allocator>::npos,
                 CharT zero = CharT(0),

                 CharT one = CharT(1));
</td>

<td > (3) </td> <td > (until C++11)



(since C++11) </td> </tr> <tr class="t-dcl ">

<td >
template< class CharT >

explicit bitset( const CharT* str,
                 typename std::basic_string<CharT>::size_type n =
                     std::basic_string<CharT>::npos,
                 CharT zero = CharT(0),

                 CharT one = CharT(1));
</td>

<td > (4) </td> <td > (since C++11) </td> </tr> Template:ddcl list end

Constructs a new bitset from one of several optional data sources:

1. Default constructor. Constructs a bitset with all bits set to zero.

2. Constructs a bitset using the bits in val. If the N is the size of the bitset and M is the number of set bits in val, then only min(N, M) bits will be included in the bitset.

3. Constructs a bitset using the characters in the std::basic_string str. An optional starting position pos and length n can be provided, as well as characters denoting alternate values for set (one) and unset (zero) bits.

The effective length of the initializing string is min(n, str.size() - pos).

If pos > str.size(), this constructor throws std::out_of_range. If any characters examined in str are not zero or one, it throws std::invalid_argument.

4. Similar to (3), but uses a CharT* instead of a std::basic_string.

Contents

Parameters

val - number used to initialize the bitset
str - string used to initialize the bitset
pos - a starting offset into str
n - number of characters to use from str
one - alternate character for set bits in str
zero - alternate character for unset bits in str

Exceptions

1. none

2. none

3. std::out_of_range if pos > str.size()

4. none


Example

#include <bitset>
#include <string>
 
int main() 
{
    // empty constructor
    std::bitset<8> b1; // [0,0,0,0,0,0,0,0]
 
    // unsigned long long constructor
    std::bitset<8> b2(42); // [0,0,1,0,1,0,1,0]
 
    // string constructor
    std::string bit_string = "110010";
    std::bitset<8> b3(bit_string);       // [0,0,1,1,0,0,1,0]
    std::bitset<8> b4(bit_string, 2);    // [0,0,0,0,0,0,1,0]
    std::bitset<8> b5(bit_string, 2, 3); // [0,0,0,0,0,0,0,1]
 
    // string constructor using custom zero/one digits
    std::string alpha_bit_string = "aBaaBBaB";
    std::bitset<8> b6(alpha_bit_string, 0, alpha_bit_string.size(),
                      'a', 'B');         // [0,1,0,0,1,1,0,1]
 
    // char* constructor using custom digits
    std::bitset<8> b7("XXXXYYYY", 8, 'X', 'Y'); // [0,0,0,0,1,1,1,1]
    return 0;
}


See also

sets bits to true or given value
(public member function) [edit]
sets bits to false
(public member function) [edit]