std::bitset::bitset

From cppreference.com
< cpp‎ | utility‎ | bitset
 
 
Utilities library
Type support (basic types, RTTI, type traits)
Dynamic memory management
Error handling
Program utilities
Variadic functions
Date and time
Function objects
(C++11)
Relational operators
Optional and any
(C++17)
(C++17)
Pairs and tuples
(C++11)
(C++17)
Swap, forward and move
(C++14)
(C++11)
(C++11)
Type operations
(C++11)
(C++17)
 
 
(1)
bitset();
(until C++11)
constexpr bitset();
(since C++11)
(2)
bitset( unsigned long val );
(until C++11)
constexpr bitset( unsigned long long val );
(since C++11)
(3)
template< class CharT, class Traits, class Alloc >

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

                     std::basic_string<CharT,Traits,Alloc>::npos);
(until C++11)
template< class CharT, class Traits, class Alloc >

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

                 CharT one = CharT('1'));
(since C++11)
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'));
(4) (since C++11)

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, initializing the first (rightmost, least significant) M bit positions to the corresponding bit values of val, where M is the smaller of the number of bits in an unsigned long long and the number of bits N in the bitset being constructed. If M is less than N (the bitset is longer than 32 (until C++11)64 (since C++11) bits, for typical implementations of unsigned long (since C++11)long), the remaining bit positions are initialized to zeroes.
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. Traits::eq() is used to compare the character values.
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. Equivalent to bitset(n == basic_string<CharT>::npos ? basic_string<CharT>(str) : basic_string<CharT>(str, n), 0, n, zero, one)

Contents

[edit] 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

[edit] Exceptions

1) none
2) none
(until C++11)
1)
noexcept specification:  
noexcept
  
2)
noexcept specification:  
noexcept
  
(since C++11)
3) std::out_of_range if pos > str.size(), std::invalid_argument if any character is not one or zero
4) std::invalid_argument if any character is not one or zero


[edit] Example

#include <bitset>
#include <string>
#include <iostream>
#include <climits>
 
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]
    std::bitset<70> bl(ULLONG_MAX); // [0,0,0,0,0,0,1,1,1,...,1,1,1] in C++11
    std::bitset<8> bs(0xfff0);      // [1,1,1,1,0,0,0,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]
 
    std::cout << b1 << '\n' << b2 << '\n' << bl << '\n' << bs << '\n'
              << b3 << '\n' << b4 << '\n' << b5 << '\n' << b6 << '\n'
              << b7 << '\n';
}

Output:

00000000
00101010
0000001111111111111111111111111111111111111111111111111111111111111111
11110000
00110010
00000010
00000001
01001101
00001111

[edit] See also

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