std::bitset::operator>,>>=(3)
π 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 π.
- , 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 π
- , 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 π
- , 3. β a new
bitsetobject containing the shifted bits β¨ - , 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
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms