function - In C++, how can I test for one of several flags combined with "|" in one argument? -


i have next code:

int main() {  ownselect(23, fd_read | fd_write); // <---- several arguments 1  return 0; }  int ownselect(socket s, long lnetworkevents) {  // how can check fd_read has been passed?  if(lnetworkevents == fd_read)  {   // never here  }  return 0; } 

how can check fd_read has been passed no matter if fd has been passed fd_read. thanks!

seems you're missing out on bit of basic bit manipulation here. you're or'ing fd_read , fd_write (| = bitwise or), thereby setting bits indicated both values, parameter. check if fd_read passed, need , lnetworkevents fd_read , check if result equals fd_read, so:

if (fd_read == (lnetworkevents & fd_read)) { ... }

this of course assuming fd_read , fd_write values meant used way (i.e. typically don't have overlapping bits).

edit: fixed, wabepper absolutely right :) oops!


Comments

Popular posts from this blog

c# - SVN Error : "svnadmin: E205000: Too many arguments" -

c++ - Using OpenSSL in a multi-threaded application -

All overlapping substrings matching a java regex -