Topic: ios_base format flags and basefield manipulators: dec, hex, oct,


Author: AlbertoBarbati@libero.it (Alberto Ganesh Barbati)
Date: Thu, 29 Mar 2007 14:52:38 GMT
Raw View
godwater@gmail.com ha scritto:
>
> Oh, I'm sorry. I mean that if we need implement one by ourselves when
> we need output as binary format. Thanks.
>

If you don't care about performances and your numbers fit in an unsigned
long, you can use bitset. For example:

  std::cout << std::bitset<32>(53);

produces

  00000000000000000000000000110101

(notice that the number of binary digits produced is equal to the number
of bits specified in the template argument).

The issue about performances is that operator<< is implemented by first
converting the number to a string and then outputting the string, thus
potentially incurring in a dynamic allocation.

Ganesh

PS: when quoting, please remove at least the moderation banner...

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: AlbertoBarbati@libero.it (Alberto Ganesh Barbati)
Date: Tue, 27 Mar 2007 17:10:50 GMT
Raw View
Steve Ward ha scritto:
> I'm curious why there is no "bin" format flag or basefield
> manipulator.  There is dec (base 10), hex (base 16), and oct (base 8),
> but no bin (understood to be base 2).
>
> Is there a good reason why bin doesn't exist?  If not, then I'm
> tempted to write a proposal.
>

Like it or not, stream I/O is eventually defined in terms of fprintf
format specifiers. oct, dec and hex are the counterparts of %o, %u (or
%d) and %x. There is no fprintf format specifier for binary. In fact
even the manipulator std::setbase(n) is only meaningful if n is either
8, 10 or 16. Whether this is a good enough reason for not having bin, I
let you decide.

Ganesh


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]