Code
#include<iostream>
#include<bitset>
int main()
{
std::ios_base::fmtflags flag=std::ios::showpos;
std::cout<<std::cout.flags()<<"\n";
std::cout<<flag<<"\n";
std::cout.flags(flag);
std::cout<<std::cout.flags()<<"\n";
std::cout<<59.20<<"\n";
std::cout<<std::bitset<16>(4098)<<"\n";
std::cout<<std::bitset<16>(2048)<<"\n";
}
Output
4098
2048
+2048
+59.2
0001000000000010
0000100000000000
see here I set showpos
flag manipulator and then I value returned by cout.flags()
(current format setting) decreased bits set.
Code
#include<iostream>
#include<bitset>
int main()
{
std::cout<<std::cout.flags()<<"\n";
std::cout<<std::showpos;
std::cout<<std::cout.flags()<<"\n";
std::cout<<59.20<<"\n";
std::cout<<std::bitset<16>(4098)<<"\n";
std::cout<<std::bitset<16>(6146)<<"\n";
}
Output
4098
+6146
+59.2
0001000000000010
0001100000000010
here as expected bits set value increased by 1
as I added one flag manipulator.
Why when I set flag with help of cout.flags()
function bit sets decrease rather than increase ?
The flags
function takes the new value, not a flag to add. What you're doing is replacing the flags entirely with showpos
.
If you want to do it with flags
, you need to add the flag yourself:
std::cout.flags(std::cout.flags() | std::ios::showpos);