std::bitset::operator>,>>=(3)

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

πŸ’… std::bitset::operator>,>>=(3)

okay bestie so this is a page from the C++ Standard Libary man pages (yes, that's how it's spelled on the tin, don't come for me 🫢) and it's all about shifting bits around in a std::bitset. Section three of the manual, periodt. ⭐


✨ NAME ✨

std::bitset::operator>,>>= β€” literally std::bitset::operator>,>>=, that's the whole name, no notes πŸ’€


🌸 Synopsis 🌸

bitset operator<<( std::size_t pos ) const; (1) (noexcept since C++11)
                                                    (constexpr since C++23)
bitset& operator<<=( std::size_t pos );     (2) (noexcept since C++11)
                                                    (constexpr since C++23)
bitset operator>>( std::size_t pos ) const; (3) (noexcept since C++11)
                                                    (constexpr since C++23)
bitset& operator>>=( std::size_t pos );     (4) (noexcept since C++11)
                                                    (constexpr since C++23)

So what's the vibe? This family performs a binary shift left (towards higher index positions) and a binary shift right (towards lower index positions). Zeroes get shifted in, and any bits that would land on an index out of range are just dropped β€” ignored, gone, not invited to the party πŸ™ˆ.

  1. , 2. β€” these two do the binary shift left. The (2) version is destructive, iykyk: it performs the shift directly on the current object, no new object for you πŸ’…
  2. , 4. β€” these two do the binary shift right. The (4) version is destructive too, i.e. it performs the shift to the current object.

πŸŽ€ Parameters πŸŽ€

  • pos β€” number of positions to shift the bits 🌈

πŸ‘‘ Return value πŸ‘‘

  1. , 3. β€” a new bitset object containing the shifted bits ✨
  2. , 4. β€” *this 🫢

πŸ¦‹ Example πŸ¦‹

// Run this code

#include <bitset>
#include <iostream>

int main()
{
    std::bitset<8> b{0b01110010};
    std::cout << b << " (initial value)\n";

    for (; b.any(); b >>= 1)
    {
        while (!b.test(0))
            b >>= 1;
        std::cout << b << '\n';
    }

    std::cout << b << " (final value)\n";
}

πŸ’– Output:

01110010 (initial value)
00111001
00000111
00000011
00000001
00000000 (final value)

like, the way it just eats its own trailing zeroes one shift at a time? obsessed 😭πŸ”₯


🌈 See also 🌈

  • rotl β€” computes the result of bitwise left-rotation (C++20) (function template) ⭐
  • rotr β€” computes the result of bitwise right-rotation (C++20) (function template) ⭐
  • operator&= β€” performs binary AND, OR, XOR and NOT (public member function) πŸ’œ
  • operator|= β€” performs binary AND, OR, XOR and NOT (public member function) πŸ’œ
  • operator^= β€” performs binary AND, OR, XOR and NOT (public member function) πŸ’œ
  • operator~ β€” performs binary AND, OR, XOR and NOT (public member function) πŸ’œ

ngl bestie, if you're shifting by an amount that sends bits out of range, they're just gone β€” dropped, ignored, no cap. that's the documented behaviour, not a bug, so don't @ me πŸ’€

okay that's the whole page, go shift some bits and slay πŸ’…βœ¨

β€” sourced from http://cppreference.com, version 2024.06.10. bye bestie, mwah πŸŽ€πŸ’–πŸ¦‹

πŸ“¦ where this page comes from