Live data from Hacker News

Why 1 && 2 == 2

blog.chewxy.com

51–58 of 58 posts

Re: Why 1 && 2 == 2

#51
post #37

I sometimes wish that x * y would short circuit on x = 0. Just because you could.

You mean, like, in the compiler? If x is actually a macro that expands to zero, that will (or at least should, for a sufficiently smart compiler) happen. Otherwise, I can’t imagine a runtime check of this being appreciably faster than hardware multiply. Right?

Re: Why 1 && 2 == 2

#52
I am not a programmer, but it seems to me the logical thing to do would be to operate on the binary values of 1 and 2.

Since 1d = 01b and 2d = 10b...

1 AND 2 = 01 AND 10 = 00 = 0

1 OR 2 = 01 OR 10 = 11 = 3

Re: Why 1 && 2 == 2

#53

I am not a programmer, but it seems to me the logical thing to do would be to operate on the binary values of 1 and 2. Since 1d = 01b and 2d = 10b... 1 AND 2 = 01 AND 10 = 00 = 0 1 OR 2 = 01 OR 10 = 11 = 3

That's kinda what binary logical operations are for. Typically AND is denoted by the symbol '&', and OR is denoted by the symbol '|'

Re: Why 1 && 2 == 2

#54
post #37

I sometimes wish that x * y would short circuit on x = 0. Just because you could.

You mean, like, in the compiler? If x is actually a macro that expands to zero, that will (or at least should, for a sufficiently smart compiler) happen. Otherwise, I can’t imagine a runtime check of this being appreciably faster than hardware multiply. Right?

I would guess he/she meant something along the lines of x * some_really_complicated_expression. If x is 0, you would not need to evaluate the other operand at all. It may even be a faster, but I doubt any compiler does this sort of optimization, since it is not safe in languages with non-pure functions. The complicated expression could change some global state or perform input/output.

E.g. in Haskell, it would be possible to do this safely, because it is guaranteed by the type system that there are no side effects. However, even with literal 0 in the code the other operand still gets evaluated.

Re: Why 1 && 2 == 2

#55
post #41
post #25

Earlier quoted context omitted.

This is commonly seen in Perl and PHP with stuff like launchMissiles() or die('failure');

Ugh, `or` is mostly useless in PHP. That code is identical to: launchMissiles() || die('failure');

That code is the same, but or has a lower priority than ||, so they are not the same is the code before the or is an expression.

Also, using or instead or || for these types of things is more readable since it makes your intent of conditional code execution (as opposed to boolean evaluation) clear.

Re: Why 1 && 2 == 2

#56

Thank god he didn't try php. echo 2 && 1; // 1 echo 1 && 2; // 1 echo 2 || 1; // 1 echo 1 || 2; // 1 echo 2 ?: 1; // 2 In php ?: is javascript's || Unfortunately there is no && that works correctly.

Came here to say this. If you use var_dump you can see it's coercing the result to a bool. $ php -r 'var_dump(1 && 2);' bool(true)

Exactly, it's not that bizarre. PHP assumes when you're using && you're going to have a boolean outcome.

The odd bit is that casting a boolean to a string[0] with echo coerces the result to (string) "1" for true and (string) "" for false.

[0]: http://www.php.net/manual/en/language.types.string.php#langu...

Re: Why 1 && 2 == 2

#57
post #55
post #41

Earlier quoted context omitted.

Ugh, `or` is mostly useless in PHP. That code is identical to: launchMissiles() || die('failure');

That code is the same, but or has a lower priority than ||, so they are not the same is the code before the or is an expression. Also, using or instead or || for these types of things is more readable since it makes your intent of conditional code execution (as opposed to boolean evaluation) clear.

Meh, it's not more unreadable unless you have issues understanding || in the first place... and "or" is still a boolean operation.

Considering 99.9% of it's use in PHP is some variant of "connect() or die()", I stand by my statement. Besides, those "connect() or die()" expressions are widely considered bad-practice in the first place.

Re: Why 1 && 2 == 2

#58
post #54

Earlier quoted context omitted.

You mean, like, in the compiler? If x is actually a macro that expands to zero, that will (or at least should, for a sufficiently smart compiler) happen. Otherwise, I can’t imagine a runtime check of this being appreciably faster than hardware multiply. Right?

I would guess he/she meant something along the lines of x * some_really_complicated_expression. If x is 0, you would not need to evaluate the other operand at all. It may even be a faster, but I doubt any compiler does this sort of optimization, since it is not safe in languages with non-pure functions. The complicated expression could change some global state or perform input/output. E.g. in Haskell, it would be pos…

Ah OK. That makes sense.
Post reply on HN