TheDocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
>> operator in C++ serves a dual architectural purpose: it is natively the bitwise right shift operator for integral types, and it is conventionally overloaded within the C++ Standard Library as the stream extraction operator for input streams.
Bitwise Right Shift Operator
At the hardware level,>> shifts the binary representation of its left operand to the right by the number of bit positions specified by its right operand.
- Type Promotion: Both operands undergo integral promotion before the shift operation. The type of the result is the type of the promoted left operand.
- Unsigned Integers (Logical Shift): If the left operand is an unsigned type, the operator performs a logical right shift. The vacated bits on the most significant (left) side are filled with zeros.
- Signed Integers (Arithmetic Shift): If the left operand is a signed type, the operator performs an arithmetic right shift. The vacated bits are filled with the value of the original sign bit (sign extension). Note: Prior to C++20, right-shifting a negative signed integer was implementation-defined. As of C++20, C++ mandates two’s complement representation, guaranteeing an arithmetic shift.
- Undefined Behavior (UB): The operation invokes UB if the right operand is negative, or if it is greater than or equal to the bit-width of the promoted left operand.
Stream Extraction Operator
The Standard Library overloads>> via std::istream::operator>> and non-member functions to facilitate formatted input parsing.
- Whitespace Handling: By default, the operator consumes and discards leading whitespace characters (spaces, tabs, newlines) before attempting to parse the target type. This behavior is governed by the
std::skipwsformat flag. - State Mutation: The operator modifies the right operand by writing the parsed value into it. If parsing fails (e.g., encountering non-numeric characters when a numeric type is expected), the right operand is left in a valid but unspecified state (or zeroed out in modern C++), and the stream’s
failbitis set. - Return Value: The operator returns a reference to the left operand (
std::istream&). This allows for left-to-right operator chaining.
Operator Characteristics
- Associativity: Left-to-right.
- Precedence: Level 7. It evaluates after arithmetic operators (
+,-) but before relational operators (<,>). This requires careful parenthesization when combining bitwise shifts with arithmetic. - Overloading: When overloading
>>for user-defined types to support stream extraction, it must be implemented as a non-member function (often afriendfunction) because the left operand is a standard library stream, not the user-defined class.
Master C++ with Deep Grasping Methodology!Learn More





