Connor M. answered • 06/20/19

Basics-First Math, Computer Science, and Exam Prep Tutor

Short answer: Yes, there is a category of inputs that will return 0, namely any a, b, and c such that a is falsey, b is falsey, and c is truthy. Any other set of inputs will return 1.

---

Long answer: First, let's clean up and simplify this expression a bit. I will use `<==>` to mean "is equivalent to".

That's what you get when you apply the precedence of operators. The unary operators `!`, `+`, and `-` are all on the same precedence level and evaluate from right to left (for example, `!!-!a === !(!(-(!a)))`). Now, notice how a, b, and c are all immediately coerced to booleans by the `!` operator. Thus, we can simplify this whole ordeal greatly by assuming a, b, and c are assigned either 0 or 1, which represent the falsey and truthy categories of inputs respectively. Another thing that makes this possible is that the bitwise operations in the expression convert `false` and `true` back to `0` and `1`. I am choosing 0 and 1 as representations of falsey and truthy values because this will make the simplified expression a lot cleaner, as we'll soon see.

With the understanding that a, b, and c can each only be 0 or 1, let's simplify this a bit more. First, let's examine the truth table for `(!a != !!b)` alongside some other expressions:

So we can simplify `(!a != !!b)` to `(a ^ !b)`. Notice that we can do that because the XOR (`^`) operator converts booleans to 0 if false and 1 if true, and the boolean expression `(!a != !!b)` ends up getting XOR'ed to something else anyway, so the numerical value will work just fine. Let's apply that simplification:

Now let's consider `!!-!a`. First, `!a` converts `a` to a boolean and then negates it. Second, the `-` operator is applied to `!a`, which converts the boolean result of `!a` to a number (either 0 or 1) and then makes it negative (either -0 or -1, respectively). Note that -0 is falsey and -1 is truthy, so the logical value doesn't change as a result. Then `!!` is applied to `-!a`, resulting in `false` if `-!a` is -0, or `true` if `-!a` is -1. The result is boolean, and since neither `-` nor `!!` inverts the truthiness of `!a`, we can simplify `!!-!a` to `!a`. Let's use this in addition to some boolean logic laws:

At this point, we can say if b is 1 (or truthy, if using the original expression), then the expression returns 1. Otherwise, the return value is `(!+!a | !c)`. Let's simplify `!+!a` first. We did something similar above. `!a` converts `a` into a boolean and negates it. Then, the `+` operator converts `!a` into a number: 0 if `!a` is false, and 1 if `!a` is true. Again, this does not invert the truthiness. Then, the leftmost `!` operator converts the number `+!a` back into a boolean and negates it. Thus the expression simplifies to `!!a`, or we can write simply `a`, because it is used as an operand in a bitwise operation `|`. We're almost there: Now we have simplified the entire expression to

b || (a | !c).

Now, since we restricted our inputs to 0 (representing all possible falsey inputs) or 1 (representing all possible truthy inputs) we can rewrite the bitwise OR as a logical OR, because the logical OR returns the truthy *value* (0 or 1), not a boolean:

That's as simple as it gets! Here's the final truth table:

Because of the simplifications we were able to make because of the boolean conversions at the beginning, you can simply look at the truthiness of the inputs and convert them to 0 if falsey or 1 if truthy to determine the output. Thus, the return value of `!a != !!b ^ !!-!a || !+!a | !c` is 0 if a is falsey and b is falsey and c is truthy, and the expression evaluates to 1 otherwise.