I know in c++ we can format the output/input using state flags like ios::showbase... I know that we can set the flag for basefield to hex,oct,dec but is there bin? and how to format integers in binary basefield?
            Asked
            
        
        
            Active
            
        
            Viewed 1,833 times
        
    3 Answers
8
            You can use bitset<>. For example:
    int x = 1025;
    std::cout << std::bitset<32>(x) << std::endl;
The above will produce the output:
00000000000000000000010000000001
        jxh
        
- 69,070
 - 8
 - 110
 - 193
 
- 
                    
 - 
                    3The C++ standard doesn't mandate it. I don't think it would be prudent for me to speculate as to why. – jxh Sep 08 '13 at 20:34
 - 
                    @Jxh which library file include definition of `bitset`, `iomanip` giving error. and why `setbase(2)` not works ? – Abhishek Mane Jun 15 '21 at 09:39
 - 
                    1@AbhishekMane: (1) `#include
` (2) `setbase()` is only defined to react to 8, 16, or 10. – jxh Jun 15 '21 at 17:48 
1
            
            
        I realize that many people are happy with the approach using a std::bitset<N> to print the relevant value. Although this sort of work, I like to point out that it is possible to write a bin manipulator although it is a bit involved: you'd derive from std::num_put<char> and install a std::locale using the corresponding facet. Below is an implementation of this approach. Look at the function print() at the bottom to see the "nice" code - the top is just making things happen with the std::nun_put<char> facet.
#include <iostream>
#include <iomanip>
#include <locale>
#include <limits>
#include <iterator>
#include <algorithm>
int bin_index(std::ios_base::xalloc());
struct num_put
    : std::num_put<char>
{
public:
    num_put(std::locale const& chain)
        : d_chain(std::use_facet<std::num_put<char> >(chain)) {
    }
private:
#if __cplusplus == 201103L
    typedef long long          long_forward;
    typedef unsigned long long actual_type;
#else
    typedef unsigned long long_forward;
    typedef unsigned long actual_type;
#endif
    std::num_put<char> const& d_chain;
    typedef std::num_put<char>::iter_type iter_type;
    iter_type do_put(iter_type to, std::ios_base& fmt, char_type fill,
                     long v) const {
        return fmt.iword(bin_index)
            ? this->put(to, fmt, fill, static_cast<long_forward>(v))
            : this->d_chain.put(to, fmt, fill, v);
    }
#if __cplusplus == 201103L
    iter_type do_put(iter_type to, std::ios_base& fmt, char_type fill,
                     long long v) const {
        return fmt.iword(bin_index)
            ? this->put(to, fmt, fill, static_cast<unsigned long long>(v))
            : this->d_chain.put(to, fmt, fill, v);
    }
    iter_type do_put(iter_type to, std::ios_base& fmt, char_type fill,
                     unsigned long v) const {
        return fmt.iword(bin_index)
            ? this->put(to, fmt, fill, static_cast<unsigned long long>(v))
            : this->d_chain.put(to, fmt, fill, v);
    }
#endif
    iter_type do_put(iter_type to, std::ios_base& fmt, char_type fill,
                     actual_type v) const {
        if (fmt.iword(bin_index)) {
            char  bits[std::numeric_limits<actual_type>::digits];
            char* end(bits);
            if (v == 0) {
                *end++ = '0';
            }
            else {
                for (; v; ++end) {
                    *end = "01"[v & 0x1u];
                    v >>= 1;
                }
            }
            std::streamsize show(2 * (bool(fmt.flags()
                                           & std::ios_base::showbase)));
            std::streamsize nofill(show + end - bits < fmt.width()
                                   ? fmt.width() - (show + end - bits)
                                   : 0);
            fmt.width(0);
            if (0 < nofill && (fmt.flags() & std::ios_base::right)) {
                end = std::fill_n(end, nofill, fill);
            }
            if (fmt.flags() & std::ios_base::showbase) {
                *end++ = 'b';
                *end++ = '0';
            }
            if (0 < nofill && (fmt.flags() & std::ios_base::internal)) {
                end = std::fill_n(end, nofill, fill);
            }
            to = std::copy(std::reverse_iterator<char*>(end),
                             std::reverse_iterator<char*>(bits),
                             to);
            if (0 < nofill && (fmt.flags() & std::ios_base::left)) {
                to = std::fill_n(to, nofill, fill);
            }
            return to;
        }
        else {
            return this->d_chain.put(to, fmt, fill, v);
        }
    }
};
std::ostream& bin(std::ostream& out)
{
    if (!dynamic_cast<num_put const*>(
            &std::use_facet<std::num_put<char> >(out.getloc()))) {
        std::locale loc(std::locale(), new num_put(out.getloc()));
        out.imbue(loc);
    }
    out.iword(bin_index) = true;
    return out;
}
std::ostream& nobin(std::ostream& out)
{
    out.iword(bin_index) = false;
    return out;
}
void print(int value)
{
    std::cout << std::right;
    std::cout << nobin << std::setw(8) << value << "="
              << bin << std::right << std::setw(20) << value << "'\n";
    std::cout << nobin << std::setw(8) << value << "="
              << bin << std::internal << std::setw(20) << value << "'\n";
    std::cout << nobin << std::setw(8) << value << "="
              << bin << std::left << std::setw(20) << value << "'\n";
}
int main()
{
    std::cout << std::showbase;
    print(0);
    print(17);
    print(123456);
}
        Dietmar Kühl
        
- 150,225
 - 13
 - 225
 - 380
 
- 
                    when i compile this code I got 20 compile-time erros; on of them: error C2632: 'long' followed by 'long' is illegal – ProDev7 Sep 08 '13 at 23:21
 - 
                    @ProDev7: It seems you are not using C++11! In that case things need to be structured slightly different. I'll update the answer as soon as I have a version doing both... – Dietmar Kühl Sep 08 '13 at 23:26
 - 
                    @ProDev7: I have updated the code to compile with both C++03 and C++11 and use the appropriate overwritten versions of `do_put()` (I think; it isn't too thoroughly tested). – Dietmar Kühl Sep 08 '13 at 23:36
 
0
            
            
        You can also add your own stream manipulator like described here:
Custom stream manipulator for streaming integers in any base