HCL three input odd parity - c

I have three inputs: a,b, and c. If my output is 1 then there are odd number of inputs with 1. Otherwise it is 0.
I have tried so far (a && b && c) || (!a && !b && !c), (a && b && c) || (!a && b && c), (a && c) || (b&& !c) and quite a few others. How can I do this?

How about a ^ b ^ c?
If only basic logic operators are allowed, you can use this
((a && b || !c) || (!a && !b || !c)) && (!a || !b || c) && (a || b || c)
as dbaupp commented, just equivalent transformation.

Related

Why no brackets needed among conditions in if sentence?

int num;
scanf("%d", &num);
if (num % 4 == 0 && num%100 != 0 || num % 400 == 0)
printf("%d", 1);
else
printf("%d", 0);
In this logic, I found I do not need to do () in AND condition which is in front of OR condition.
if (*(num % 4 == 0 && num%100 != 0)* || num % 400 == 0)
It's only needed if (num % 4 == 0 && num%100 != 0 || num % 400 == 0) without () in front of OR condition.
so, it seems (A && B || C) works like ((A && B) || C)
but it seemed it could work as (A && (B || C)) condition.
Why is () not needed in this situation? A and B condition are automatically grouped from the beginning?
All operators in C (and in fact all languages) have what's called operator precedence which dictates which operands are grouped first.
The logical AND operator && has a higher precedence than the logical OR operator ||, which means that this:
A && B || C
Is the same as this:
(A && B) || C
So if you want B || C to be grouped together, you need to explicitly add parenthesis, i.e.:
A && (B || C)
Parentheses decide order of operations, if you move the parentheses around you may change what the output is. In the same way that (A + B) / C is different from A + (B / C) but still a valid equation.
See Order of Operations in C
Logical AND has higher priority than OR:
https://en.cppreference.com/w/c/language/operator_precedence
dg

Simplifying (and make it more legible) logic condition

I am looking for help in making this logic more legible. Assume each alphabet letter is a compare statement (e.g TRUE == a.foo). Each alphabet is about 30 char long statements.
if ( ((a || b)
&& (c || d)) ||
((e || f)
&& (g || h)) )
Any suggestions?
Decompose it.
int ab = a || b,
cd = c || d,
ef = e || f,
gh = g || h,
firstThing = ab && cd,
secondThing = ef && gh;
if (firstThing || secondThing)
Try lining up the subexpressions in groups, lining up the parenthesis:
if (((a || b) && (c || d)) ||
((e || f) && (g || h)))
In order for the conditions to align properly and the logic operator to stand out, I would use this style:
if (((a || b) && (c || d))
|| ((e || f) && (g || h))
...
|| ((u || v) && (w || x))) {
/* handle the successful test */
} else {
/* handle the other cases */
}
There are a few variations of this, but here's one way that I think is pretty clear:
if
(
(
(a || b)
&&
(c || d)
)
||
(
(e || f)
&&
(g || h)
)
)

Why does this following program print "Yes" instead of "No"?

Why does this following program print "Yes" instead of "No"?
None of the variables is initialized to 2.
bool hello = 0;
int a = 1;
int b = 3;
int c = 4;
int d = 5;
if (a || b || c || d == 2) {
hello = 1;
}
if (hello == 1) {
printf("Yes");
}
if (hello == 0) {
printf("No");
}
return 0;
}
The statement
if (a || b || c || d == 2)
is equivalent to:
if (a != 0 || b != 0 || c != 0 || d == 2)
The equality comparison does not automatically distribute across all the variables. If you want to do that, you need to perform all the comparisons explicitly:
if (a == 2 || b == 2 || c ==2 || d == 2)
The expression (a || b || c || d == 2) evalutates to true because it treats a, b, c as booleans, and any non-zero integer is true.
You have given logical operator in the expression It means that if non zero value came then the expression is true. Then hello=1 is set and in next f statement it prints YES
You just meet the short circuit behavior of logical expressions OR.
The order of evaluation of logical OR || is left to right.
So in the following expression:
left || right
if left = true then right will never going to be executed (short circuit). In your code exactly same happened.
As you know, any non zero value treated as true in C, hence, a which is 1 is true. So, take a look:
if (a || b || c || d == 2)
if (true || bla bla bla) //rights are not even checked!
if (true)
hello = 1;
Tada! So the program print "Yes"!
None of the variables is initialized to 2.
Yes of course! But your if condition is not going to check that. To do so, try this:
if (a == 2 || b == 2 || c ==2 || d == 2) {
//...
because if judge num is not zero , if think this is true. so your code
if (a || b || c || d == 2)
like
if ( true || true || true || false)
the result is true, programe print "YES"

Trying to assign specific chars as a variable in C...giving me some difficulty

So I am trying to assign a char variable with the value "#" or "%" or "!" and if the variable does not have that value, I am prompting the user with an error. While compiling, I am getting the error "error: comparison between pointer and integer". Now, my code where the error is happening is this segment:
if (((a == !) || (a == %) || (a == #)) && (w > 0 && h > 0)) {
//do something
}
I can't figure out for the life of me why an error is coming up here. Any thoughts would be greatly appreciated.
Chars in C must be surrounded by single quotes:
if (((a == '!') || (a == '%') || (a == '#')) && (w > 0 && h > 0))

"lvalue required as left operand of assignment " error

The following code produces a "lvalue required as left operand of assignment"
if( c >= 'A' && c <= 'Z' || c = " " || c = ",") {
I assume I'm writing this wrong, what is wrong? and how would I write it correctly?
You should use single quotes for chars and do double equals for equality (otherwise it changes the value of c)
if( c >= 'A' && c <= 'Z' || c == ' ' || c == ',') {
Furthermore, you might consider something like this to make your boolean logic more clear:
if( (c >= 'A' && c <= 'Z') || c == ' ' || c == ',') {
Although your boolean logic structure works equivalently (&& takes precedence over ||), things like this might trip you up in the future.
equality is ==, = is assignment. You want to use ==. Also "" is a char*, single quotes do a character.
Also, adding some parens to your condition would make your code much easier to read. Like so
((x == 'c' && y == 'b') || (z == ',') || (z == ' '))
= is the assigning operator, not the comparing operator. You are looking for ==.
Personally, I prefer the minimalist style:
((x == 'c' && y == 'b') || (z == ',') || (z == ' '))
( x == 'c' && y == 'b' || z == ',' || z == ' ' )
or
( x == 'c' && y == 'b' ? z == ',' : z == ' ' )
against
( x == 'c' && y == 'b' ? z == ',' : z == ' ')

Resources