
New C++ experimental feature: The tadpole operators - ingve
http://blogs.msdn.com/b/oldnewthing/archive/2015/05/25/10616865.aspx
======
copsarebastards
This is a joke. For those who didn't get the joke:

1\. ~ and - are separate operators. - negates, ~ does this:
[http://stackoverflow.com/a/7207406](http://stackoverflow.com/a/7207406)

2\. Integers on computers are typically represented in two's complement
notation:
[http://www.cs.cornell.edu/~tomf/notes/cps104/twoscomp.html](http://www.cs.cornell.edu/~tomf/notes/cps104/twoscomp.html)

3\. Now that we know how all the parts work, we can put them together step by
step:

    
    
        ~-0010 => ~1110 => 0001
        -~0010 => -1101 => 0011

------
jloughry
It's a side effect of two's complement representation of integers. There ought
to be a repository somewhere for neat tricks like this (with a warning never
to use them in production).

    
    
        FOR ENTERTAINMENT PURPOSES ONLY

~~~
kyteland
There is, and I can't recommend it highly enough.

[http://www.amazon.com/Hackers-Delight-2nd-Henry-
Warren/dp/03...](http://www.amazon.com/Hackers-Delight-2nd-Henry-
Warren/dp/0321842685/)

~~~
nightcracker
Was going to recommend Hacker's Delight, and saw I was too late :)

~~~
avz
One of the "tadpole" operators is actually on the cover right above "Delight".

------
daeken
For those saying that this is unreadable, I really think that the trigraph
alternative is for you! Instead of _~-_ and _-~_ , you can use _??--_ and
_-??-_. IMO, those are much clearer.

------
Sniffnoy
The joke has already been explained in the comments there, yet people are
still posting comments there oblivious of it...

~~~
jerf
I really can't figure out why so many people read The Old New Thing, or
perhaps more accurately, feel compelled to comment in the comment section.
This post is perfectly typical of the level of technical comprehension
necessary to enjoy the blog. You are routinely expected to (after explanation
at least) be able to understand COM, or the details of the Windows message
pump, or how C++'s deallocation interacts with Windows process shutdown, or
how threading interacts with destructors, or as in this case, get the joke
without having it explained for you, but the comment sections are always
populated by dozens of people who are just lost as can be. I don't know how
they get there in such quantity, or why they seem to collectively stay. (Do
they get to the blog via google searches and stick around because they think
it's a place to complain about Windows? It's certainly at least one popular
pasttime....)

I'm grateful that Raymond Chen seems to have a lot of patience for it, because
The Old New Thing is a unique gem and would be irreplaceable if shut down, so
I'm glad he keeps at it. May his patience stay strong.

~~~
plorkyeran
A decent number of his posts are fascinating historical stories that don't
require deep technical knowledge to enjoy, so I assume those draw in readers
that stick around despite not understanding most of what he posts.

~~~
RobertKerans
Yeah, I go back to it a lot for this reason, despite having little of the
necessary knowledge to enjoy the more technical posts - I would never comment,
but I can understand fully why people [often näively] do

------
avz
This is of course a joke. Here is why it holds in general. If x is signed
number in two's complement representation then

    
    
        x + ~x = -1
    

because x has a 1 where ~x has a 0 and vice versa and because all ones
11...111 in two's complement always represents -1 [ * ].

Transformation of the last formula yields

    
    
        x + 1 = -~x
    

The other "tadpole" operator can be derived from the above one by setting x =
-y

    
    
        -y + 1 = -~-y
        y - 1 = ~-y
    

[ * ] This follows from the fact that the sum of all powers of two from 0th to
(n-1)th is equal to 2^n - 1. Now, if the sign of the most significant bit in
all-ones binary number 11..111 is flipped then the sum becomes -1.

------
cpeterso
Are there any other named C++ pseudo-operators? For example, the "clown eyes"
operator !! commonly is used to coerce a boolish value to a bool. :)

~~~
mewse
In Mac programming (pre-OSX) it was common for the OS to give you memory
blocks not using a pointer, but using a 'handle', which under Mac OS<=9 meant
"a pointer to a pointer" (where the direct pointer was under OS control so
that it could move memory blocks around in low-memory situations. By
comparison, a 'handle' in Windows is a "memory block the contents of which you
don't know what they are". On Mac OS<=9, you did know the contents; you just
had to double-dereference your pointer to reach them)

This meant that when accessing data through these handles, you had to write
code like this:

    
    
        (*handle)->field;
    

or

    
    
        (**handle).field;
    

Eventually, someone coined a pseudo-operator for reaching fields from a handle
without requiring parentheses:

    
    
        handle[0]->field;  
    

[0]-> was named the "sproing" operator (on the theory that it indicates the
desired value popping out of a box).

~~~
asveikau
> By comparison, a 'handle' in Windows is a "memory block the contents of
> which you don't know what they are".

This is incorrect.

In Windows, the HANDLE is a typedef for a void pointer, but its usage is
usually 100% analogous to a file descriptor - i.e. it is a token passed to
kernel space, where the kernel looks it up in a per-process table of open
files. I say "usually" because some components have re-used the HANDLE typedef
for things which are not kernel objects - for example the HANDLE that comes
from FindFirstFile() is actually a pointer to a structure in user-space [which
AFAIK itself has a HANDLE to the directory].

~~~
ms2
That's true for some handles in Win32, but (since we're talking about classic
MacOS) Win16 handles were usually NEAR pointers inside kernel segments.

~~~
JoeAltmaier
True! That was a disaster; only 64K (16 bits) handles were possible across all
processes. Many APIs had to be deprecated and redesigned when it went 32-bit.

I've argued for using uuids for handles from here on out. They are endless; no
effort to coordinate handle spaces is ever needed; they work locally and on
the network; on some architectures they are even a register width scalar.

------
whoopdedo

        That problem will go away once the optimizer is taught to recognize this operator. Then we could call it the rocket-powered tadpole. -Raymond
    

It seems GCC (4.9.2) already recognizes the operator and generates optimal
instructions.

------
im3w1l
Edit: It's a joke but also teaching you a trick. I like.

------
watmough
Strong force '~' finally makes it into C++.

------
phamilton
The tadpole operator has been supported in JavaScript, Ruby, and Python for
years.

~~~
TazeTSchnitzel
Yet another case of C++ lagging behind superior languages.

------
andrewchambers
Horror followed by lolling.

~~~
andrewchambers
To the friendly people downvoting this - Horror at seeing microsoft would do
something so crazy, laughing after realizing I had been tricked by two's
complement.

------
shultays
Reminds me the "goes to" operator

    
    
        int x = 10;
        while (x --> 0) printf("%d\n", x);
    

prints 9 ... 0. The operator is pronounced as "as x goes to 0"

------
alanmorph
MS sure got a lot of serious hater... Why so serious?

------
jcoffland
The sperm operator (as it should be called) wont work if your vars are
unsigned ints, floats or doubles.

------
smegel
Very good. Back to work now.

------
halayli
I wish they introduce "in" as an operator.

~~~
halayli
instead of down voting and running away, provide constructive feedback as to
why you think this isn't a good idea.

------
kps
Pfft, trust Microsoft to push nonportable code.

~~~
Kronopath
Au contraire, mon ami: this feature is supported on all major compilers. ;)

~~~
CamperBob2
I'm pretty sure that C doesn't guarantee a two's-complement representation.
Not so sure about C++. Either way, it makes for a cute novelty article, and a
really terrible idea.

Raymond should've saved it for the next April Fools' Day.

~~~
dllthomas
It's supported by all major compilers, but some may have buggy implementations
if you try to target a non-two's-complement architecture. Clearly they need to
fix those bugs!

~~~
CamperBob2
Right, the tilde operator is supported, but the math trick in question only
works on a twos-complement machine. So it's non-portable in the sense that the
language spec doesn't guarantee a particular behavior.

~~~
dllthomas
Yup. If it wasn't clear, my comment above was a joke :-P

------
static_noise
What demented mind invented this? It's not posted on April 1st so I'm not sure
it's meant as a joke. Also it's on msdn.com.

Tilde and minus look just alike and make reading the code much harder if used
together. "Look there's a --i oh no, it's actually -~i or is it ~-i or ~~i?"

~~~
dllthomas
It's just regular unreadable code. ~ and - are both prefix operators: bitwise
complement and negation, respectively. With two's-complement arithmetic,
"complement then negate" adds one while "negate then complement" subtracts
one.

~~~
Gibbon1
I got that feeling again when I was trying to learn 6502 assembly.

Amusingly at least in gcc

#\\\ this appears legal

