I was playing the Javascript game with somebody and we were having fun making ridiculous and absurd expressions to make our inputs get a particular output.
This little charming one
!a!=!!b^!!-!a||!+!a|!c
always seemed to return 1
. I tried to reason it out, but I gave up after losing track of all the !
s.
Are there any values for a
, b
, and c
which do not return 1
? If not, why does it always return 1
?
No, you can not return multiple values like this in C. A function can have at most one single return value.
No, you can't return multiple types.
The report_ratio function has a void return type, so it doesn't need to explicitly return a value. Execution of report_ratio "falls off the bottom" and returns no value to the caller.
return 1 in the main function means that the program does not execute successfully and there is some error. return 0 means that the user-defined function is returning false. return 1 means that the user-defined function is returning true.
Short answer, yes. a = false, b = false, c = true
is a counter-example because your equation is identical to (!!a || !!b || !c)
.
Long answer:
!a!=!!b^!!-!a||!+!a|!c
is
(((!a) != (!!b)) ^ (!!(-!a))) || ((!+!a)|!c)
which reduces to
((Boolean(a) == Boolean(b)) ^ (!a)) || (Boolean(a) | !c)
so all of a
, b
and c
are only dealt with as truthy/falsey values and the result must be a 1
or 0
since |
and ^
both coerce booleans to numbers.
So obviously (from inspection of the right of the ||
) if either a
is truthy or c
is falsey, you get 1
.
If a
is falsey and c
is truthy, you have two possibilities,
b
is truthy in which case the ^
clause is 1
so the right of the ||
is never reached.b
is falsey, in which case the ^
clause is 0
so the right of the ||
dominates to produce 0
.How about this:
var a = undefined, b=undefined, c=!a alert(!a!=!!b^!!-!a||!+!a|!c) // Output: 0
Live demo.
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