Namespaces
Variants
Views
Actions

std::bitset<N>::operator<<,<<=,>>,>>=

From cppreference.com
< cpp‎ | utility‎ | bitset
 
 
Utilities library
General utilities
Relational operators (deprecated in C++20)
Integer comparison functions
(C++20)(C++20)(C++20)
(C++20)
Swap and type operations
(C++14)
(C++11)
(C++11)
(C++11)
(C++17)
Common vocabulary types
(C++11)
(C++17)
(C++17)
(C++17)
(C++11)
(C++17)

Elementary string conversions
(C++17)
(C++17)
Stacktrace
 
 
(1)
bitset operator<<( std::size_t pos ) const;
(until C++11)
bitset operator<<( std::size_t pos ) const noexcept;
(since C++11)
(2)
bitset& operator<<=( std::size_t pos );
(until C++11)
bitset& operator<<=( std::size_t pos ) noexcept;
(since C++11)
(3)
bitset operator>>( std::size_t pos ) const;
(until C++11)
bitset operator>>( std::size_t pos ) const noexcept;
(since C++11)
(4)
bitset& operator>>=( std::size_t pos );
(until C++11)
bitset& operator>>=( std::size_t pos ) noexcept;
(since C++11)

Performs binary shift left and binary shift right. Zeroes are shifted in.

1-2) Performs binary shift left. The (2) version is destructive, i.e. performs the shift to the current object.
3-4) Performs binary shift right. The (4) version is destructive, i.e. performs the shift to the current object.

Contents

[edit] Parameters

pos - number of positions to shift the bits

[edit] Return value

1,3) new bitset object containing the shifted bits
2,4) *this

[edit] Example

#include <iostream>
#include <bitset>
 
int main()
{
    std::bitset<8> b{0b01110010};
    std::cout << b << " (initial value)\n";
 
    for (; b.any(); b >>= 1) {
        for (; !b.test(0); b >>= 1) {
        }
        std::cout << b << '\n';
    }
}

Output:

01110010 (initial value)
00111001
00000111
00000011
00000001

[edit] See also

(C++20)
computes the result of bitwise left-rotation
(function template) [edit]
(C++20)
computes the result of bitwise right-rotation
(function template) [edit]
performs binary AND, OR, XOR and NOT
(public member function) [edit]