tags:

views:

94

answers:

2

Hi, I'm playing at the moment with bits and was looking at microsoft code for io states and discovered something like this:

enum _Iostate
 { // constants for stream states
 _Statmask = 0x17};//What is this mask for???

static const _Iostate goodbit = (_Iostate)0x0;
static const _Iostate eofbit = (_Iostate)0x1;
static const _Iostate failbit = (_Iostate)0x2;
static const _Iostate badbit = (_Iostate)0x4;
static const _Iostate _Hardfail = (_Iostate)0x10;

I just wonder why is this mask for because code works without this mask and values stayed the same with as whitout this mask. Thank you.

+6  A: 

It makes sure that the _Iostate enum has the correct size to hold all the bit constants defined afterward, and their combinations.

sth
+1. More precisely, the range of an enumeration is the range of values that can be stored in the smallest possible bitfield storing all enumerators of the enumeration. In this case, the range is `0 .. 31`.
Johannes Schaub - litb
Great stuff, thank you
There is nothing we can do
+3  A: 

That's a bitwise OR of all possible flags. You could perhaps use it to extract the part containing the flags from other bits in the integer.

sharptooth
Great stuff, thank you.
There is nothing we can do
he accepted an answer, but I believe this answer is the correct one
karoberts
+1 Yeah, this is probably the more correct one. But none of these two is wrong, i think each of them explain an important part. For instance, this answer doesn't explain why that enumerator isn't defined like the other 5 ones outside the enumeration.
Johannes Schaub - litb
He said "code works without this mask" which would imply that it's unused (or at least that was my interpretation), but I haven't looked at the code.
sth
I guess he meant that existing code work without it and I see no reason for some code to be bound to that constant. But if one wants to pack several bitmasks ito an integer he could use such constants for masking diring unpacking and for estimating the number of bits required.
sharptooth
Since those constants are used in templates (standard library streams), it doesn't surprise me it works in VC++. He will probably receive an error once calling some function making use of it (maybe `rdflags()`, `fail()`, etc...).
Johannes Schaub - litb