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
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.
& 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)
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:
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.
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...
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.