fork download
  1. #include <iostream>
  2. #include <bitset>
  3.  
  4. int main()
  5. {
  6. constexpr std::size_t NBOOLS = 5 ;
  7. // http://e...content-available-to-author-only...e.com/w/cpp/utility/bitset
  8. std::bitset<NBOOLS> group ; // group of 5 bits (or 5 boolean values)
  9.  
  10. auto zero = group[0] ;
  11. auto one = group[1] ;
  12. auto two = group[2] ;
  13. auto three = group[3] ;
  14. auto four = group[4] ;
  15.  
  16. two = true ;
  17. three = true ;
  18.  
  19. std::cout << std::boolalpha ;
  20. for( std::size_t i = 0 ; i < NBOOLS ; ++i )
  21. std::cout << "group[" << i << "] == " << group[i] << '\n' ;
  22.  
  23. std::cout << "group: " << group << '\n' ;
  24.  
  25. // is any of the boolean values true? (is any bit set?)
  26. if( group.any() ) std::cout << "atleast one of the " << group.size() << " is true (set)\n" ;
  27.  
  28. // are all of them false? (is no bit set?)
  29. if( group.none() ) std::cout << "none of the " << group.size() << " are true (set)\n" ;
  30.  
  31. // how many are true (set)?
  32. std::cout << group.count() << " of the " << group.size() << " are true (set)\n" ;
  33.  
  34. zero = true ; // set zero to true
  35. three = false ; // set three to false
  36. four.flip() ; // toggle four
  37. std::cout << "group: " << group << '\n' ;
  38. std::cout << group.count() << " of the " << group.size() << " are true (set)\n" ;
  39.  
  40. // etc.
  41. }
  42.  
Success #stdin #stdout 0s 3476KB
stdin
Standard input is empty
stdout
group[0] == false
group[1] == false
group[2] == true
group[3] == true
group[4] == false
group: 01100
atleast one of the 5 is true (set)
2 of the 5 are true (set)
group: 10101
3 of the 5 are true (set)