Namespaces
Variants

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

From cppreference.net
Utilities library
bitset & operator & = ( const bitset & other ) ;
(1) (自 C++11 起为 noexcept)
(自 C++23 起为 constexpr)
bitset & operator | = ( const bitset & other ) ;
(2) (自 C++11 起为 noexcept)
(自 C++23 起为 constexpr)
bitset & operator ^ = ( const bitset & other ) ;
(3) (自 C++11 起为 noexcept)
(自 C++23 起为 constexpr)
bitset operator~ ( ) const ;
(4) (自 C++11 起为 noexcept)
(自 C++23 起为 constexpr)

执行二进制 AND、OR、XOR 和 NOT 运算。

1) 将位设置为对 * this other 的对应位对执行二进制 AND 操作的结果。
2) 将位设置为对 * this other 的对应位对执行二进制 OR 操作的结果。
3) 将位设置为 * this other 对应位对进行二进制异或运算的结果。
4) 返回 * this 的临时副本,其中所有位均被翻转(二进制 NOT 运算)。

请注意, &= |= ^= 运算符仅针对相同大小 N 的位集定义。

目录

参数

其他 - 另一个位集

返回值

1-3) * this
4) std:: bitset < N > ( * this ) . flip ( )

示例

#include <bitset>
#include <cstddef>
#include <iostream>
#include <string>
int main()
{
    const std::string pattern_str{"1001"};
    std::bitset<16> pattern{pattern_str}, dest;
    for (std::size_t i = dest.size() / pattern_str.size(); i != 0; --i)
    {
        dest <<= pattern_str.size();
        dest |= pattern;
        std::cout << dest << " (i = " << i << ")\n";
    }
    std::cout << ~dest << " (~dest)\n";
}

输出:

0000000000001001 (i = 4)
0000000010011001 (i = 3)
0000100110011001 (i = 2)
1001100110011001 (i = 1)
0110011001100110 (~dest)

参见

执行二进制左移与右移操作
(公开成员函数)