std::bitset...or&=,|=,^=,~(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸŽ€ std::bitset::operator&=,|=,^=,~ πŸŽ€

okay bestie, welcome to the <bitset> operator page from the C++ Standard Libary πŸ’– β€” the header files it under std::bitset...or&=,|=,^=,~(3), and we are keeping every funky character. This one is all about smashing bits together with binary AND, OR, XOR, and NOT. periodt. ✨


🌸 NAME 🌸

std::bitset::operator&=,|=,^=,~ - std::bitset::operator&=,|=,^=,~

✨ Synopsis ✨

bitset& operator&=( const bitset& other ); (1) (noexcept since C++11) (constexpr since C++23)
bitset& operator|=( const bitset& other ); (2) (noexcept since C++11) (constexpr since C++23)
bitset& operator^=( const bitset& other ); (3) (noexcept since C++11) (constexpr since C++23)
bitset operator~() const;                  (4) (noexcept since C++11) (constexpr since C++23)

This crew performs binary AND, OR, XOR and NOT. 🫢

  1. Sets the bits to the result of binary AND on corresponding pairs of bits of *this and other.
  2. Sets the bits to the result of binary OR on corresponding pairs of bits of *this and other.
  3. Sets the bits to the result of binary XOR on corresponding pairs of bits of *this and other.
  4. Returns a temporary copy of *this with all bits flipped (binary NOT).
🚨 Note that &=, |=, and ^= are only defined for bitsets of the same size N. Same size or it’s not it, bestie.

πŸ’– Parameters πŸ’–

  • other β€” another bitset

πŸŽ€ Return value πŸŽ€

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

πŸ¦‹ Example πŸ¦‹

Run this code, bestie:

// Run this code

#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";
}

🌟 Output 🌟

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

πŸ”₯ See also πŸ”₯

  • operator<<=, operator>>= β€” performs binary shift left and shift right
  • operator<< β€” (public member function)
  • operator>>

mwah πŸ’‹ β€” source: http://cppreference.com, version/date: 2024.06.10, page: std::bitset...or&=,|=,^=,~(3). slay and stay bitwise, bestie πŸ‘‘πŸ’œ

πŸ“¦ where this page comes from