7 ms·
New C++ experimental feature: The tadpole operators
- watmough 12y agoStrong force '~' finally makes it into C++.
- im3w1l 12y agoEdit: It's a joke but also teaching you a trick. I like.
- jloughry 12y agoIt'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 12y agoThere is, and I can't recommend it highly enough. http://www.amazon.com/Hackers-Delight-2nd-Henry-Warren/dp/0321842685/ http://www.amazon.com/Hackers-Delight-2nd-Henry-Warren/dp/03...
- nightcracker 12y agoWas going to recommend Hacker's Delight, and saw I was too late :)
- avz 12y agoOne of the "tadpole" operators is actually on the cover right above "Delight".
- fmela 12y agoI own this book, and it is fantastic. There's also HAKMEM[1] and the FXT book[2]. [1]: http://www.inwap.com/pdp10/hbaker/hakmem/hakmem.html http://www.inwap.com/pdp10/hbaker/hakmem/hakmem.html [2]: http://www.jjj.de/fxt/ http://www.jjj.de/fxt/
- Sniffnoy 12y agoThe joke has already been explained in the comments there, yet people are still posting comments there oblivious of it...
- jerf 12y agoI 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 12y agoA 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 12y agoYeah, 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
- undertow 12y agoHis replies to some of the comments wink at the possibility that he thoroughly enjoys the rancor and consternation stirred in the minds of that particular subset of his readers. e.g. That problem will go away once the optimizer is taught to recognize this operator. Then we could call it the rocket-powered tadpole. - Raymond Consider the people, who haven't commented, instead simply skimming this post, without reading deeper, then showing up to work on Monday, and dutifully explaining to colleagues about a new operator they learned of.
- kps 12y agoPfft, trust Microsoft to push nonportable code.
- Kronopath 12y agoAu contraire, mon ami: this feature is supported on all major compilers. ;)
- dwulfen 12y agothis is effectively bitwise math, so it's actually supported in any language offering the minus and binary complement prefix operators. Drop this into your address bar: javascript:var%20x=100;alert("x="+x+"\nx-1="+(~-x)+"\nx+1="+(-~x));
- CamperBob2 12y agoI'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 12y agoIt'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 12y agoRight, 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 12y agoYup. If it wasn't clear, my comment above was a joke :-P
- static_noise 12y agoWhat 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 12y agoIt'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 12y agoI got that feeling again when I was trying to learn 6502 assembly. Amusingly at least in gcc #\\ this appears legal
- deleted 12y ago[deleted]
- nanofortnight 12y agoSometimes I can't tell if people are being serious about their objections to this or if they are willingly oblivious.
- zamalek 12y agoIndeed. It's more interesting that an explanation (top comment) was posted one hour before this objection to the "feature."
- ultimape 12y agoRaymond Chen rants about other developers doing stupid things - his job is basically to help them get stuff done in 'the right way' from what I can tell, so he finds a lot of weirdness like this. My impression is that this is a parody of something ridiculous he found.
- halayli 12y agoI wish they introduce "in" as an operator.
- halayli 12y agoinstead of down voting and running away, provide constructive feedback as to why you think this isn't a good idea.
- daeken 12y agoFor 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.
- cpeterso 12y agoAre there any other named C++ pseudo-operators? For example, the "clown eyes" operator !! commonly is used to coerce a boolish value to a bool. :)
- zem 12y agoi'd call that the bunny operator because it reminds me of the sanrio bunnies.
- im3w1l 12y agoThere is the down-to pseudo-operator --> as in int x = 3; while(x --> 0) { std::cout<<x<<"\n"; }
- repsilat 12y agoIn addition to the rightward arrow, there's the leftward arrow: int x = 3; while(n <-- x) { cout << x << endl; } which is different because it doesn't include the lower bound `n`. And of course, if typing the ">" is too taxing, the "---" operator is equivalent to the "-->" operator: int x = 3; while(x --- n) { cout << x << endl; } A few more goodies in this style at http://steike.com/code/useless/evil-c/ http://steike.com/code/useless/evil-c/ (home of the zip-quine.)
- dustmop 12y agoA crying face is called "ever" #define ever (;;) for ever { // do something }
- mewse 12y agoIn 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).
- deleted 12y ago[deleted]
- whoopdedo 12y agoThat 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.
- deleted 12y ago[deleted]
- alanmorph 12y agoMS sure got a lot of serious hater... Why so serious?
- andrewchambers 12y agoHorror followed by lolling.
- andrewchambers 12y agoTo 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.
- smegel 12y agoVery good. Back to work now.
- copsarebastards 12y agoThis 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
- phamilton 12y agoThe tadpole operator has been supported in JavaScript, Ruby, and Python for years.
- TazeTSchnitzel 12y agoYet another case of C++ lagging behind superior languages.
- avz 12y agoThis 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.
- jcoffland 12y agoThe sperm operator (as it should be called) wont work if your vars are unsigned ints, floats or doubles.
- shultays 12y agoReminds 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"