##### cppreference.com

##### Search

Create account

##### Namespaces

- Page
- Discussion

##### Variants

##### Views

- View
- Edit
- History

##### Actions

# Standard library header <bitset>

From cppreference.com
< cpp‎ | header

This header is part of the general utility library.

### Includes
`<string>`
`<iosfwd>`
### Classes
bitset | implements constant length bit array (class) [edit]
std::hash<std::bitset> (C++11) | hash support for std::bitset (class template specialization) [edit]
### Functions
operator&operator|operator^ | performs binary logic operations on bitsets (function template) [edit]
operator<<operator>> | performs stream input and output of bitsets (function template) [edit]

### [edit] Synopsis

    #include <string>
    #include <iosfwd> // for istream, ostream

    namespace std {
        template <size_t N> class bitset;

        // bitset operators:
        template <size_t N>
            bitset<N> operator&(const bitset<N>&, const bitset<N>&) noexcept;
        template <size_t N>
            bitset<N> operator|(const bitset<N>&, const bitset<N>&) noexcept;
        template <size_t N>
            bitset<N> operator^(const bitset<N>&, const bitset<N>&) noexcept;
        template <class charT, class traits, size_t N>
            basic_istream<charT, traits>&
            operator>>(basic_istream<charT, traits>& is, bitset<N>& x);
        template <class charT, class traits, size_t N>
            basic_ostream<charT, traits>&
            operator<<(basic_ostream<charT, traits>& os, const bitset<N>& x);

        // Hash support
        template <class T> struct hash;
        template <size_t N> struct hash<bitset<N> >;

    }

#### [edit] Class std::bitset

    template<size_t N> class bitset {
    public:
        // bit reference:
        class reference {
            friend class bitset;
            reference() noexcept;
        public:
            ~reference() noexcept;
            reference& operator=(bool x) noexcept;          //  for b[i] = x;
            reference& operator=(const reference&) noexcept;//  for b[i] = b[j];
            bool operator~() const noexcept;                //  flips the bit
            operator bool() const noexcept;                 //  for x = b[i];
            reference& flip() noexcept;                     //  for b[i].flip();

        };

        //constructors:
        constexpr bitset() noexcept;
        constexpr bitset(unsigned long long val) noexcept;
        template<class charT, class traits, class Allocator>
            explicit bitset(
                const basic_string<charT,traits,Allocator>& str,
                typename basic_string<charT,traits,Allocator>::size_type pos = 0,
                typename basic_string<charT,traits,Allocator>::size_type n =
                    basic_string<charT,traits,Allocator>::npos,
                charT zero = charT('0'), charT one = charT('1'));
        template <class charT>
            explicit bitset(
                const charT* str,
                typename basic_string<charT>::size_type n = basic_string<charT>::npos,
                charT zero = charT('0'), charT one = charT('1'));

        // bitset operations:
        bitset<N>& operator&=(const bitset<N>& rhs) noexcept;
        bitset<N>& operator|=(const bitset<N>& rhs) noexcept;
        bitset<N>& operator^=(const bitset<N>& rhs) noexcept;
        bitset<N>& operator<<=(size_t pos) noexcept;
        bitset<N>& operator>>=(size_t pos) noexcept;
        bitset<N>& set() noexcept;
        bitset<N>& set(size_t pos, bool val = true);
        bitset<N>& reset() noexcept;
        bitset<N>& reset(size_t pos);
        bitset<N> operator~() const noexcept;
        bitset<N>& flip() noexcept;
        bitset<N>& flip(size_t pos);

        // element access:
        constexpr bool operator[](size_t pos) const; // for b[i];
        reference operator[](size_t pos); // for b[i];

        unsigned long to_ulong() const;
        unsigned long long to_ullong() const;
        template <class charT = char,
                class traits = char_traits<charT>,
                class Allocator = allocator<charT> >
            basic_string<charT, traits, Allocator>
            to_string(charT zero = charT('0'), charT one = charT('1')) const;

        size_t count() const noexcept;
        constexpr size_t size() noexcept;

        bool operator==(const bitset<N>& rhs) const noexcept;
        bool operator!=(const bitset<N>& rhs) const noexcept;

        bool test(size_t pos) const;
        bool all() const noexcept;
        bool any() const noexcept;
        bool none() const noexcept;

        bitset<N> operator<<(size_t pos) const noexcept;
        bitset<N> operator>>(size_t pos) const noexcept;
    };

Retrieved from "http://en.cppreference.com/mwiki/index.php?title=cpp/header/bitset&oldid=69984"