0

Having the follow code -

    enum FileOpenFlags {
    flagREAD = 1, flagWRITE = 2,
    flagCREATE = 4, flagEND = 8,
    flagAPPEND = flagWRITE | flagEND,
    };
    cout << flagAPPEND << endl;

gives 10 . can you explain me what the | did ?

URL87
  • 10,667
  • 35
  • 107
  • 174
  • Yes, I can. But I won't as you should have read a C(++) tutorial instead of bugging me with this. –  Aug 28 '12 at 18:15
  • Those `flag` prefixes can be easily avoided with scoped enums in C++11. – chris Aug 28 '12 at 18:15
  • 1
    @H2CO3 all my google searches gave results about another meaning of "pipe" , sorry about bugging you . – URL87 Aug 28 '12 at 18:33
  • That's because it's not a pipe. It's an inclusive or operator. – Pete Becker Aug 28 '12 at 20:04
  • @PeteBecker, but some will know it as a pipe *character*. I can easily see where the confusion comes from, and I'm glad to have a place on StackOverflow for Google to find it. – Mark Ransom Aug 30 '12 at 14:09

4 Answers4

5

It did a bitwise or of the two values.

Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
4

flagWRITE's (2) binary representation is 0010

flagEND's (8) binary representation is 1000

0010 OR 1000 gives you 1010 which equals 10

mostar
  • 4,723
  • 2
  • 28
  • 45
2

It's called Bitwise OR........

Seva Alekseyev
  • 59,826
  • 25
  • 160
  • 281
2

It's a bitwise "OR" operator. So the bit value of 2 and 8 respectively get OR'd bitwise.

So:

   1000 (flagEND = 8) 
OR 0010 (flagWRITE = 2)
-----------
 = 1010 (flagAppend = 10)
Ivory_Tower
  • 183
  • 1
  • 1
  • 8