~- is known as the inchworm on a stick operator in Perl obfuscation circles. It isn't really an operator; it's a term coined to describe negating a value and then taking its compliment, the apparent effect of which is to decrement the value. (To understand why, you need to understand how negative numbers are represented in binary.)
In Perl, ~- works for positive values:
Interestingly enough, when the same is tried in PHP the results are slightly different...
PHP treats the value as a signed integer where Perl treats it as an unsigned integer. I don't know which way is "better" or which is "more correct," but it is something I found interesting and I thought would be worth sharing.
In Perl, ~- works for positive values:
for ($i = 3; $i > -3; $i--) { print ~-$i . "\n"; } $ perl test.pl 2 1 0 4294967295 4294967294 4294967293
Interestingly enough, when the same is tried in PHP the results are slightly different...
2 1 0 -1 -2 -3
PHP treats the value as a signed integer where Perl treats it as an unsigned integer. I don't know which way is "better" or which is "more correct," but it is something I found interesting and I thought would be worth sharing.
So why the difference? Clearly it isn't dependent on the processor architecture. Also it makes me wonder if there is a way to modify the default behavior in either case.
ReplyDeleteInteresting post!