How bitwise AND works for Class Variables?

In the following program how bitwise AND work for class variable? As we know: if(7 && 8) evalutes TRUE whereas if(7 & 8) evalutes FALSE. What are the bit values of "f" and "ios::showpos" and how IF verify these two variables?


#include <iostream>
using namespace std;

int main()
{
ios::fmtflags f = cout.flags();

if(f & ios::showpos)
cout <<"showpos is set for cout. \n";
else
cout <<"showpos is cleared for cout \n";

cout <<"\nSetting showpos for cout. \n";
cout.setf(ios::showpos);

f = cout.flags();

if(f & ios::showpos)
cout <<"showpos is set for cout. \n";
else
cout <<"showpos is cleared for cout. \n";

return 0;

}


OUTPUT IS:

showpos is cleared for cout.

Setting showpos for cout.
showpos is set for cout.


& Is bitwise and. In this context, showpos is a value which has all bits clear except for one. IE: it is a mask to isolate that specific bit in f.

The & operation will be nonzero only if the bit showpos specifies is set in f.

Further reading:
http://www.cplusplus.com/forum/beginner/72705/#msg387899
Topic archived. No new replies allowed.