Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compare Boolean Variables in one statement as long as array C++

Lets say I have an boolean array with 5 bool variables all being true

bool boo[10];
for(int i = 0; i < 5; i++){
    boo[i] = true;
}

I want them all compared into one NAND logic gate at once because my problem is if I always compare two variables and compare the merged boolean to the i+1 boolean variable. This gives a wrong result.

    bool NANDGate(bool array[]){
    bool at;
    for(int i = 1; i < 5; i++){
        if(i == 1){
            at = !(array[i-1] && array[i]);
        }else{
            at = !(at && array[i]);
        }
    }
    return at;
}
// result here is true even though it should be false

What I want is a right result when I put every variable from boo into a NAND gate so something maybe that looks like this:

bool func(bool array[]){
// some loop
result = !(array[0] && array[1] && array[2] && array[3] && array[4]);
return result;
}
// result here would be false

actually it doesn't have to look like that just maybe a solution that has a right result like the above would have.

EDIT: So many great solutions I thank you all

like image 244
HaskellPlease Avatar asked Jan 10 '19 16:01

HaskellPlease


People also ask

What is the size of a Boolean variable in C++?

The above output results as storing any value in a boolean variable other than 0, will result in 1 being stored in that variable. The size of boolean data type in C++ is 1 byte, whereas size of boolean in Java is not precisely defined and it depends upon the Java Virtual Machine (JVM).

How to create a boolean array in C?

We can also create the boolean arrays using the data type bool from the stdbool.h header file in C. The boolean array can be used to store multiple true or false values for each of its elements.

How to check if a Boolean variable is true or false?

In the above example, we have included the stdbool.h header file as we are using the bool type variable in our program, then we have declared a variable x of type boolean using bool and initialized it with value false. After that, we applied the condition checks using the if-else block to determine if the value of variable x is true or false.

How to declare a Boolean data type in C?

To declare a boolean data type in C we have to use a keyword named bool followed by a variable name. bool var_name; Here, keyword bool is the data type of variable and var_name is the variable. A bool takes in real 1 bit, as we need only 2 different values (0 or 1).


2 Answers

If you accept a C++17 solution, you can make all constexpr using an helper function and template folding as follows

#include <iostream>
#include <utility>
#include <type_traits>

template <std::size_t N, std::size_t ... Is>
constexpr bool NANDhelper (bool const (&a)[N],
                           std::index_sequence<Is...> const &)
 { return ! (a[Is] && ...); }

template <std::size_t N>
constexpr bool NANDgate (bool const (&a)[N])
 { return NANDhelper(a, std::make_index_sequence<N>{}); }

int main ()
 {
   bool a[] { true, true, true, true, true };
   bool b[] { true, false, true, true, true };

   std::cout << NANDgate(a) << std::endl;
   std::cout << NANDgate(b) << std::endl;
 }

If you can't use C++17, but at least C++14, you can't use template folding but you can simulate it in an unused array initialization; something as follows

template <std::size_t N, std::size_t ... Is>
constexpr bool NANDhelper (bool const (&a)[N],
                           std::index_sequence<Is...> const &)
 {
   using unused = bool[];

   bool val { true };

   (void)unused { true, (val &= a[Is])... };

   return ! val; 
 }

Unfortunately std::index_sequence and std::make_index_sequence are available only starting from C++14 so, if you want something similar, you have to emulate them (and NANDhelper() can't be, in C++11, constexpr).

like image 152
max66 Avatar answered Sep 21 '22 21:09

max66


replace your definition by :

 bool NANDGate(bool array[]){
   bool at = array[0];
   for(int i = 1; i < 5; i++){
     at &&= array[i];
   }
   return !at;
 } 

The not (!) must be made at the end to be compatible with !(array[0] && array[1] && array[2] && array[3] && array[4]);

And in your definition you also take into account some entries 2 times

But to do the && up to the end is useless, the best is just to do :

 bool NANDGate(bool array[]){
   for(int i = 0; i < 5; i++){
     if (!array[i])
       return true;
   }
   return false;
 } 
like image 35
bruno Avatar answered Sep 18 '22 21:09

bruno