Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can !a!=!!b^!!-!a||!+!a|!c return anything other than 1?

Tags:

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?

like image 520
Peter Olson Avatar asked Aug 12 '11 19:08

Peter Olson


People also ask

Can a function return more than one value using the return statement in C?

No, you can not return multiple values like this in C. A function can have at most one single return value.

Can AC function return different types?

No, you can't return multiple types.

Which return type Cannot return any value to the Cal?

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.

What happens if you return 1 in C?

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.


2 Answers

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,

  1. b is truthy in which case the ^ clause is 1 so the right of the || is never reached.
  2. b is falsey, in which case the ^ clause is 0 so the right of the || dominates to produce 0.
like image 159
Mike Samuel Avatar answered Sep 19 '22 15:09

Mike Samuel


How about this:

var a = undefined, b=undefined, c=!a alert(!a!=!!b^!!-!a||!+!a|!c) // Output: 0 

Live demo.

like image 41
Gerrat Avatar answered Sep 22 '22 15:09

Gerrat