I noticed that although the standard library has I/O manipulators to print numbers in either decimal, hexadecimal, or octal form (std::dec
, std::hex
, std::oct
), it does not have one for binary.
How would I go about implementing an analogous binary I/O manipulator?
Anti-duplicate-marking section: I am quite dissapointed that I have to do this, but my questions almost always get marked as duplicates of completely different and unhelpful questions. Perhaps this will help people stay away from false duplicates and find the real duplicates, if they do exist.
- I know how to print a number's binary representation. That is a trivial task for anyone who knows how numbers are stored in a computer, and not the purpose of this question (i.e. implementing
std::string to_binary(T value)
,void print_binary(T value)
, ...). - I know that it is easy for anyone who has memorized the hexadecimal values to convert from hex to binary in their head. But any time you introduce human work, you introduce human capacity for error. Always best to avoid it if possible.
- I realize there are rules about what you are allowed to do with respect to extending the standard library, though I don't know the inner workings of streams and manipulators well enough to know exactly what's allowed in this case. I'm not looking for a cop-out "you can't do that" answer - if the exact analog can't be created, then give me the cleanest allowed solution you can think of that is allowed.