Namespaces
Variants

operator&,|,^ (std::bitset)

From cppreference.net
Utilities library
定义于头文件 <bitset>
template < std:: size_t N >

std:: bitset < N > operator & ( const std:: bitset < N > & lhs,

const std:: bitset < N > & rhs ) ;
(1) (C++11 起为 noexcept)
(C++23 起为 constexpr)
template < std:: size_t N >

std:: bitset < N > operator | ( const std:: bitset < N > & lhs,

const std:: bitset < N > & rhs ) ;
(2) (C++11 起为 noexcept)
(C++23 起为 constexpr)
template < std:: size_t N >

std:: bitset < N > operator ^ ( const std:: bitset < N > & lhs,

const std:: bitset < N > & rhs ) ;
(3) (C++11 起为 noexcept)
(C++23 起为 constexpr)

对两个位集 lhs rhs 执行二进制与、或、异或操作。

1) 返回一个 std:: bitset < N > ,包含 lhs rhs 对应位进行二进制AND操作的结果。
2) 返回一个 std:: bitset < N > ,包含对 lhs rhs 对应位执行二进制或操作的结果。
3) 返回一个 std:: bitset < N > ,包含 lhs rhs 对应位进行二进制异或操作的结果。

目录

参数

lhs - 运算符左侧的位集
rhs - 运算符右侧的位集

返回值

1) std:: bitset < N > ( lhs ) & = rhs
2) std:: bitset < N > ( lhs ) | = rhs
3) std:: bitset < N > ( lhs ) ^ = rhs

示例

#include <bitset>
#include <iostream>
int main()
{
    std::bitset<4> b1("0110");
    std::bitset<4> b2("0011");
    std::cout << "b1 & b2: " << (b1 & b2) << '\n';
    std::cout << "b1 | b2: " << (b1 | b2) << '\n';
    std::cout << "b1 ^ b2: " << (b1 ^ b2) << '\n';
}

输出:

b1 & b2: 0010
b1 | b2: 0111
b1 ^ b2: 0101

参见

执行二进制按位与、或、异及和非运算
(公开成员函数)