The tadpole operators explained

Raymond Chen

Last time,¹ I introduced the tadpole operators. As you have probably figured out by now, it was a joke. There are no new tadpole operators.

But the sample code works. What’s going on?

The tadpole operators are pseudo-operators, like the goes to operator or the sproing operator: They take advantage of existing language features, and come with a creative story.

The tadpole operators exploit two’s complement arithmetic and overflow.² The __ENABLE_EXPERIMENTAL_TADPOLE_OPERATORS is just a red herring.

Start with the identity for two’s complement negation

-x = ~x + 1

then move the -x to the right hand side and the ~x to the left hand side:

-~x = x + 1

If that was too fast for you, we can do it a different way: start with the identity for two’s complement negation

-x = ~x + 1

subtract 1 from both sides

-x - 1 = ~x

and finally, negate both sides

x + 1 = -~x

To get the decrement tadpole operator, start with

-x = ~x + 1

and substitute x = -y:

-(-y) = ~-y + 1

subtract 1 from both sides and simplify -(-y) to y.

y - 1 = ~-y

Update: Justin Olbrantz (Quantam) and Ben Voigt provide a simpler derivation, starting with the identity for two’s complement negation.

-x = ~x + 1
Rearrange terms ~x = -x - 1
Let x = ~y Let x = -y
-~y = ~(~y) + 1 ~-y = -(-y) - 1
-~y = y + 1 ~-y = y - 1

¹Why didn’t I post it on April 1st? Well, for one thing, April 1st is overcrowded. Second, it would have interfered with the run-up to the //build conference. And third, yesterday was a holiday in the United States, and I tend to schedule lighter fare on holidays.

²This means that they don’t work on a machine that does not use two’s complement, or one which checks overflow. Still, maybe they’ll be useful if you’re entering the IOCCC or some other contest which values minimal code size or obfuscation (or both).

0 comments

Discussion is closed.

Feedback usabilla icon