c++11 标准模板(STL)(std::bitset)(四)

定义于头文件 <bitset>

template< std::size_t N >
class bitset;

 类模板 bitset 表示一个 N 位的固定大小序列。可以用标准逻辑运算符操作位集,并将它与字符串和整数相互转换。

bitset 满足可复制构造 (CopyConstructible) 及可复制赋值 (CopyAssignable) 的要求。

模板形参

N - 要为 bitset 分配存储的位数

成员类型

reference

表示到一个位的引用的代理类
(类)


修改器

进行二进制与、或、异或及非

std::bitset<N>::operator&=,|=,^=,~

bitset<N>& operator&=( const bitset<N>& other );

(1) (C++11 前)

bitset<N>& operator&=( const bitset<N>& other ) noexcept;

(C++11 起)

bitset<N>& operator|=( const bitset<N>& other );

(2) (C++11 前)

bitset<N>& operator|=( const bitset<N>& other ) noexcept;

(C++11 起)

bitset<N>& operator^=( const bitset<N>& other );

(3) (C++11 前)

bitset<N>& operator^=( const bitset<N>& other ) noexcept;

(C++11 起)

bitset<N> operator~() const;

(4) (C++11 前)

bitset<N> operator~() const noexcept;

(C++11 起)

 行二进制与、或、异或及非。

1) 设置位为 *thisother 的位的对应对上二进制与的结果。

2) 设置位为 *thisother 的位的对应对上二进制或的结果。

3) 设置位为 *thisother 的位的对应对上二进制异或的结果。

4) 返回翻转所有位的 *this 的临时副本(二进制非)。

注意 &= 、 |= 、 和 ^= 仅对拥有相同大小 N 的 bitset 定义。

参数

other - 另一 bitset

返回值

1-3) *this

4) bitset<N>(*this).flip()

进行二进制左移和右移

std::bitset<N>::operator<<,<<=,>>,>>=

bitset<N> operator<<( std::size_t pos ) const;

(1) (C++11 前)

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

(C++11 起)

bitset<N>& operator<<=( std::size_t pos );

(2) (C++11 前)

bitset<N>& operator<<=( std::size_t pos ) noexcept;

(C++11 起)

bitset<N> operator>>( std::size_t pos ) const;

(3) (C++11 前)

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

(C++11 起)

bitset<N>& operator>>=( std::size_t pos );

(4) (C++11 前)

bitset<N>& operator>>=( std::size_t pos ) noexcept;

(C++11 起)

进行二进制左移和二进制右移。移入零。

1-2) 进行二进制左移。 (2) 是破坏性的,即对当前对象进行迁移。

3-4) 进行二进制右移。 (4) 是破坏性的,即对当前对象进行迁移。

参数

pos - 移动位的位置数

返回值

1,3) 含有被迁移位的新 bitset 对象

2,4) *this

调用示例

#include <iostream>
#include <bitset>
#include <string>

template<size_t _Nb>
void printBitset(const std::string &name, const std::bitset<_Nb> &bitset)
{
    std::cout << name << ":  ";
    for (size_t index = 0; index < bitset.size(); index++)
    {
        std::cout << bitset[index] << " ";
    }
    std::cout << std::endl;
}

int main()
{
    std::cout << std::boolalpha;
    std::bitset<8> bitset1("101010");
    std::bitset<8> bitset2("100011");
    std::cout << "bitset1:              " << bitset1 << std::endl;
    std::cout << "bitset2:              " << bitset2 << std::endl;
    //1) 设置位为 *this 与 other 的位的对应对上二进制与的结果。
    std::cout << "bitset1 & bitset2:    " << (bitset1 & bitset2) << std::endl;
    //2) 设置位为 *this 与 other 的位的对应对上二进制或的结果。
    std::cout << "bitset1 | bitset2:    " << (bitset1 | bitset2) << std::endl;
    //3) 设置位为 *this 与 other 的位的对应对上二进制异或的结果。
    std::cout << "bitset1 ^ bitset2:    " << (bitset1 ^ bitset2) << std::endl;
    //4) 返回翻转所有位的 *this 的临时副本(二进制非)。
    std::bitset<8> bitset3 = ~bitset1;
    std::cout << "~bitset1:             " << bitset3 << std::endl;
    std::cout << std::endl;


    //进行二进制左移和二进制右移。移入零。
    //1-2) 进行二进制左移。 (2) 是破坏性的,即对当前对象进行迁移。
    std::bitset<8> bitset5("101010");
    std::bitset<8> bitset6 = bitset5 << 3;
    std::cout << "bitset5:              " << bitset5 << std::endl;
    std::cout << "bitset6:              " << bitset6 << std::endl;
    std::bitset<8> bitset7 = bitset5 <<= 3;
    std::cout << "bitset5:              " << bitset5 << std::endl;
    std::cout << "bitset7:              " << bitset7 << std::endl;

    //进行二进制左移和二进制右移。移入零。
    //3-4) 进行二进制右移。 (4) 是破坏性的,即对当前对象进行迁移。
    std::bitset<8> bitset8("101010");
    std::bitset<8> bitset9 = bitset8 >> 3;
    std::cout << "bitset8:              " << bitset5 << std::endl;
    std::cout << "bitset9:              " << bitset9 << std::endl;
    std::bitset<8> bitset10 = bitset8 >>= 3;
    std::cout << "bitset8:              " << bitset5 << std::endl;
    std::cout << "bitset10:             " << bitset10 << std::endl;
    std::cout << std::endl;

    return 0;
}

输出

猜你喜欢

转载自blog.csdn.net/qq_40788199/article/details/131037011
今日推荐