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
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).
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.
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.
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).
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
).
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With