c++ - How are state flags represented and how bitwise OR is used to work with bit flags? -


if open file reading, may define 1 or more state flags, example: ios::out ios::out | iso::app

i read bitwise or, , how "merges" 2 bit sets,

for example: 1010 | 0111 = 1111

now being said, not understand how works "behind scenes" when use method ifstream.open(filename, stateflaga | stateflagb | stateflagc) , on.

can elaborate more on inner workings of these state flags , memory representation?

edit: give more emphasis on trying understand (if helps), assume open method receive 1 or more state flags separate arguments in signature, , not delimited bitwise or, want understand how bitwise or works on these state flags produce different final state when combining several flags, , result allows me use 1 argument state flag or set of state flags. ie:

ifstream.open(filename, stateflaga | stateflagb | stateflagc) 

and not

ifstream.open(filename, stateflaga , stateflagb , stateflagc) 

if take gnu libstdc++ implementation , @ how these implemented, find:

enum _ios_openmode  {    _s_app        = 1l << 0,   _s_ate        = 1l << 1,   _s_bin        = 1l << 2,   _s_in         = 1l << 3,   _s_out        = 1l << 4,   _s_trunc      = 1l << 5,   _s_ios_openmode_end = 1l << 16  }; 

these values used this:

typedef _ios_openmode openmode;  static const openmode app =     _s_app;  /// open , seek end after opening. static const openmode ate =     _s_ate;  /// perform input , output in binary mode (as opposed text mode). /// not think is; see /// http://gcc.gnu.org/onlinedocs/libstdc++/manual/bk01pt11ch27s02.html static const openmode binary =  _s_bin;  /// open input.  default @c ifstream , fstream. static const openmode in =      _s_in;  /// open output.  default @c ofstream , fstream. static const openmode out =     _s_out;  /// open input.  default @c ofstream. static const openmode trunc =   _s_trunc; 

since values chosen 1 << n, 1 "bit" each, allows combine using | (or) - other similar operations.

so app in binary 0000 0001 , bin 0000 0100, if app | bin mode opening file, 0000 0101. internals of impplementation of fstream can use

 if (mode & bin) ... stuff binary file ...  

and

 if (mode & app) ... stuff appending file ... 

other c++ library implementations may choose different set of bit values each flag, use similar system.


Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -