Difficulty: Easy
Correct Answer: the bitwise OR operator (|)
Explanation:
Introduction / Context:
C++ iostreams use bitmask-style flags to control formatting of numbers and text (e.g., base, justification, show base/prefix). Developers often need to enable multiple options at once, so it is important to know how to combine flags correctly and idiomatically.
Given Data / Assumptions:
Concept / Approach:
Bitmask flags combine using bitwise operations. To enable several independent options simultaneously, use the bitwise OR operator (|) to set multiple bits in the mask. Logical operators (||, &&) evaluate to boolean values and are not suitable for constructing bit patterns. Bitwise AND (&) is used to test or clear bits, not to combine options to be turned on together.
Step-by-Step Solution:
Choose desired flags, for example std::ios::hex and std::ios::showbase.Combine them as std::ios::hex | std::ios::showbase.Apply: stream.setf(std::ios::hex | std::ios::showbase);Verify the stream prints numbers in hexadecimal with base prefix (e.g., 0x).
Verification / Alternative check:
Consult documentation showing std::ios::fmtflags as a bitmask type and examples using the | operator for combination.
Why Other Options Are Wrong:
Common Pitfalls:
Accidentally mixing format fields that are mutually exclusive (e.g., std::ios::dec vs std::ios::hex). Use unsetf or setf with field masks to avoid conflicts.
Final Answer:
the bitwise OR operator (|).
Discussion & Comments