program tha obeys the rules of logic gates(AND, OR)

Hi,I have been trying to write a program that obeys the operations of logic gates(AND and OR).I'm currently doing Bsc Computer Sciance and Electronics (second year).My electronics lecturer gave us problem that we heard to solve using logic gates.
problem statement:Suppose that Tom wants to go to a party,but firstly he has to ask for permission from his parents.Either one of his parents has to agree and also ask one of his friends to collect him on their way to the party (one of his friends also has to agree0
True = 1
False = 0

Tom's father (Agree = True/Disagree = False)
Tom's mother (Agree = True/ disagree = False)
First friend (Agree = True/ Disagree = False)
Second friend (Agree = True/ Disagree = False)

I'm trying to write a program that solves the problem using the laws/operations of AND and OR gates.
Last edited on
Alright. You got the formula, yet?
I am not sure I have got what you mean completely but I think this may be helpful:

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
#include <iostream>
#include <string>

using namespace std;
int main(){
    string names[4] = {"father" , "mother" , "friend1" , " friend2"};
    int opinion[4];
    string input;
    for(int i = 0 ; i < 4 ; i++){
            cout<<"Enter the " << names[i] << "'s opinion: " << endl;
            getline(cin,input);
            if(input == "Agree") opinion[i] = 1;
            else if(input == "Disagree") opinion[i] = 0;
            else{
                cout<<"Wrong Input, You have to agree or disagree";
                return 0;
            }
    }
    if((opinion[0] || opinion[1]) && (opinion[2] || opinion[3])){
                   cout << "Tom can go to the party";
    }
    else
        cout << " Sorry! Tom can't go to the party";
    return 0;
}
                                                                  
What you need is actually what minmit said...

From electronics you would say:
We have four inputs, x (father), y (mother), z(friend1), w(friend2). And one output a (can, or cann't go)
Lets make the logic table from that:
x y z w a
0 0 0 0 0
0 0 0 1 0
0 0 1 0 0
0 0 1 1 0
0 1 0 0 0
0 1 0 1 1
0 1 1 0 1
0 1 1 1 1
1 0 0 0 0
1 0 0 1 1
1 0 1 0 1
1 0 1 1 1
1 1 0 0 1
1 1 0 1 1
1 1 1 0 1
1 1 1 1 1


Making the Karnaugh map you would have:

\xy
\ 00 01 11 10
zw \ -------------------
00 | 0 | 0 | 0 | 0 |
+----+----+----+----+
01 | 0 | 1 | 1 | 1 |
+----+----+----+----+
11 | 0 | 1 | 1 | 1 |
+----+----+----+----+
10 | 0 | 1 | 1 | 1 |
-------------------


And the equation from it would be: a = wy + xz + zy + zx

Simplifying it you would have:
a = wy + xz + zy + zx = z(x + y) + w(x + y) = (z + w)(x + y)


So you now need your user to enter the 4 inputs and then calculate if he can go or not lwith the above formula.
minmit's program does that. You can change the
int opinion[4]; to bool opinion[4];
and
opinion[i] = 1; to opinion[i] = true;

opinion[i] = 0; to opinion[i] = false;

if it helps you understand it better using boolean values.

Hope this helps
Last edited on
Actually, this is very simple matter:
x(mother),y(father),z(friend1),w(friend2)
(x|y)&(z|w)--> if true you can go to your party, if false you can not.

PS:

"& != &&" !!!
"| != ||" !!!
& is bitwise and
&& is logical and
| is bitwise or
|| is logical or

If you use bool values you don't have any difference because they have values of either 0 or 1.
If you don't use bool values then you should use logical operators (if you don't need bitwise operations of course...)

My previous reply was just how he could find the result formula working the truth table and the Karnaugh Map (because he is in electronics class)

Last edited on
If you use bool values you don't have any difference because they have values of either 0 or 1.
But as long as a 1 bit variable doesn't exist in C++ it isn't the same and it isn't compiled the same way.
C++ automatically converts true into 1 and false into 0.
That means that
A bool true value will have a value of integer 1. Or in bits:
00....000001 (32 bits, 31 zeros, 1 one)
A bool false value will have a value of integer 0. Or in bits:
00....000000 (32 bits, 32 zeros)

If you do the logical operator AND you will have:
true && false = false

If you do the bitwise AND operator you will have

00....000000
& 00....000001
--------------
00....000000


Which will be bool false again.
That's why I said that in bool variables it has no difference.

In Integers it will have difference. Because the logical AND (and OR the same) will convert them to boolean values taking everything different than 0 to be true and everything 0 to be false.
That means that the numbers 2 and 4 will be true, and in a logical AND they will produce a true as a result.
But the bitwise AND will make a bitwise comparison:


00....000010
& 00....000100
--------------
00....000000

Which is a boolean false.

That's why they have different behaviour at integers but not in booleans...

I don't know if the compiler will produce different code (probably it will) but it is all on how you want to use them and what will have the result you wish.

Uhh...
Actually, for conditional expressions, logical operators are preferable. You can take advantage of short circuit evaluation, and there are certain situations where bitwise operators give unintuitive results:
10&35==0, but 10&&35==!0
Now, would you please explain what advantage there is for using bitwise instead of logical in this case.

EDIT: Damn. Beaten to it.
Last edited on
10&3 != 0...


10--> 00....001010
3--->& 00....000011
------------
00....000010 <--- 2 (not 0, then true)


Wrong calculations :D
At the beginning I had 2 and 3 which has the same result... That's why I have 2 and 4... :D
Last edited on
Oops. I meant 5. I was never any good at bin2dec.
I don't know if the compiler will produce different code (probably it will) but it is all on how you want to use them and what will have the result you wish.
If you have such a simple situation bitwise operations are preferable, because they are faster. You have no jumps and just one comparison. You could also declare your variable as an unsigned char to improve program even more.
Another difference is that the bitwise operators will stop at the position of the msb, the comparision on the other hand will compare every single bit.
So they show a different behavior in integers and booleans, because booleans are usually declared as an unsigned int and your processor doesn't know that you just want to use the first bit.
Another difference is that the bitwise operators will stop at the position of the msb, the comparision on the other hand will compare every single bit.

The bitwise will for sure do all the bits...
In computer programming, a bitwise operation operates on one or two bit patterns or binary numerals at the level of their individual bits.
From Wikipedia

Your unsigned char might have a point because it is just 8bits instead of 32 of the integer.

[EDIT] I don't know but I suppose that the logical operations will convert the value to bool and then check only the LSD (Least Significant Digit) that is the only important bit...
Last edited on
bitwise operators will stop at the position of the msb

What? Bitwise operators are called so precisely because they operate every bit in a given value. They can't just skip bits.
just one difference between comparison and licical and/or/xor:
page 414 (cmp), page 418(locical operations)
http://www.intel.com/design/processor/manuals/248966.pdf
I can't find it.
In any case, mentioning a particular architecture in a discussion about general C++ is something akin to the Godwin's Law. You cannot predict that that behavior will be implemented in another architecture, so by optimizing for one, you may be deoptimizing for another. It is instead preferable to keep the code as general as possible.
Besides, optimizing conditionals is just silly. Unless of course you really need those few extra nanoseconds or bytes of code.
Topic archived. No new replies allowed.