Can you group Booleans?

If I had a list of 5 booleans.

1
2
3
4
5
bool one = false;
bool two = false;
bool three = false;
bool four = true;
bool five = false;


Is there anyway I can put them into a group ? so I could say
1
2
3
if (groupName == true){
   //actions
}


So if just one of the booleans in that group is true it will take action?
Last edited on
Um, no? What do you mean by "group"?

You can create a bool variable called groupName that is the logical or of all of the bool variables.

bool groupName = (one || two || three || four || five);

Likewise, you could use logical and if you want the the action to occur if all of them are true:

bool groupName = (one && two && three && four && five);

Not sure if that is what you mean by "group" or not, though it does meet your requirement. Your variable names are invalid by the way.
No, not the way you want. You can get closer however by using a function.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

bool allBoolsAreTrue(bool boolArray[], int length)
  {
      int true_count = 0;
      for(int i = 0; i <= length; i++)
      {
          if(boolArray[i] == true)
          {
              true_count += 1;
          }
      }
     if(true_count == length)
     {
        return true;
     }
   return false;
}

bool myBools[5];

myBools[0] = false;
myBools[1] = false;
myBools[2] = false;
myBools[3] = true;
myBools[4] = false;

if(allBoolsAreTrue(myBools,5) == true)
  {
      //stuff
  }
Sorry really poor example of my variable names I just threw them in quick, ill change the OP.

Well basically I am going to have 10 - 20 different bool variables. I will have 2 or 3 different actions, rather than do them all individually I would like to do something like this:

1
2
3
4
5
6
7
8
9
10
11
12
//Will subtract if any these bools are true (lets say these as a group would be called groupSubtract)
//groupSubtract (if there is a variable or function to group these)
bool one = false;
bool five = false;
bool seven = true;

//Will devide if any of the bools are true (lets say these as a group would be called groupDevide)
//groupDevide
bool nine = false;
bool twelve = true;
bool eight = false;
bool  three = false;


I want to be able to say

1
2
3
if (groupSubtract == true){
     //actions will happen if ANY of the bools in groupSubtract are true
}


same goes for devide
Not the way you are trying. You might want to look into std::bitset though, it can do what you want. Of course then you don't have individual names for each boolean.
Ah damn :( obviously the example I gave was just using simple names of numbers to demonstrate my point , I really need individual bool names within my program as I will have tons of conditions based on them individually , "grouping" them was my solution but it appears as if that is not really an option :(.

I guess a work around is to write a for loop with a ton of || statements and just have a bool groupname switch to true if one of them is true, although thats kinda messy , hmmm
Last edited on
You could stick your bools in a class?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// assumes C++11
// could uses accessors/mutators possible?
class Example {
public:
    //groupSubtract
    bool one = false;
    bool five = false;
    bool seven = true;

    //groupDivide
    bool nine = false;
    bool twelve = true;
    bool eight = false;
    bool three = false;

    bool groupSubtract() {
        return (one || five || seven);
    }

    bool groupDivide() {
        return (three || eight || nine || twelve);
    }
};


then use

1
2
3
4
5
6
7
Example ex;

ex.five = true; // etc

if (ex.groupSubtract() == true){
     //actions will happen if ANY of the bools in groupSubtract are true
}


Andy
Last edited on
A bool has just two values, true or false. Though usually these use a whole byte each, technically, it needs just a single bit. You could use the 32 individual bits of an integer. But that would likely end up with code which is pretty hard to understand.

Perhaps a better solution would be to define a class (or struct) to hold all of the bool values. Then you could use friendly names for each variable, and suitable member functions to test groups of values. This way, the main code becomes simplified as the member functions take care of all the complicated stuff.

Edit: I see Andy has already suggested the use of a class, which I think is a good idea.

1
2
3
if (ex.groupSubtract() == true) {
     // etc.
}

the test == true is not really needed, you could just put:
1
2
3
if (ex.groupSubtract()) {
     // etc.
}
Last edited on
> If I had a list of 5 booleans.
> Is there anyway I can put them into a group ? so I could say ...
> So if just one of the booleans in that group is true it will take action?

We can represent each boolean value as a bit in a std::bitset<>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
#include <bitset>

int main()
{
    constexpr std::size_t NBOOLS = 5 ;
    // http://en.cppreference.com/w/cpp/utility/bitset
    std::bitset<NBOOLS> group ; // group of 5 bits (or 5 boolean values)

    auto zero = group[0] ;
    auto one = group[1] ;
    auto two = group[2] ;
    auto three = group[3] ;
    auto four = group[4] ;

    two = true ;
    three = true ;

    std::cout << std::boolalpha ;
    for( std::size_t i = 0 ; i < NBOOLS ; ++i )
        std::cout << "group[" << i << "] == " << group[i] << '\n' ;

    std::cout << "group: " << group << '\n' ;

    // is any of the boolean values true? (is any bit set?)
    if( group.any() ) std::cout << "atleast one of the " << group.size() << " is true (set)\n" ;

    // are all of them false? (is no bit set?)
    if( group.none() ) std::cout << "none of the " << group.size() << " are true (set)\n" ;

    // how many are true (set)?
    std::cout << group.count() << " of the " << group.size() << " are true (set)\n" ;

    zero = true ; // set zero to true
    three = false ; // set three to false
    four.flip() ; // toggle four
    std::cout << "group: " << group << '\n' ;
    std::cout << group.count() << " of the " << group.size() << " are true (set)\n" ;

    // etc.
}

http://ideone.com/DVVxrO

EDIT: On second thoughts, this should not work. (Even though it does on my implementation):
1
2
auto zero = group[0] ;
// etc 

IIRC, std::bitset<>::reference does not have an accessible constructor, and we would have to just use the subscript operator instead.

EDIT 2: No, just checked it. auto zero = group[0] ; is just fine. Only the default constructor is declared private; the implicitly defaulted copy-constructor and move constructor would be public.
Last edited on
edit: I didn't read your second post, just make a function.

1
2
3
4
5
6
7
8
9
10
bool function(bool v1, //ect...)
{
bool groupName = true;

if (v1 == false|| //add in others)
{
groupName = false;
}
return groupName;
}

Last edited on
Topic archived. No new replies allowed.