Post #101,239
5/10/03 10:01:19 PM
|
Ooh, he left out the ^
and a line
void swap_int(int& x, int& y) { x ^= y; y ^= x; x ^= y; }
"Packed like lemmings into shiny metal boxes. Contestants in a suicidal race." - Synchronicity II - The Police
|
Post #101,253
5/10/03 11:37:38 PM
|
That's just evil to the core. :-)
|
Post #101,316
5/11/03 5:43:45 PM
|
It's C++, what do you expect?
"Packed like lemmings into shiny metal boxes. Contestants in a suicidal race." - Synchronicity II - The Police
|
Post #101,413
5/12/03 8:43:53 AM
|
I would only use that if it were properly commented.
Is it enough to love Is it enough to breathe Somebody rip my heart out And leave me here to bleed
| | Is it enough to die Somebody save my life I'd rather be Anything but Ordinary Please
| -- "Anything but Ordinary" by Avril Lavigne. |
|
Post #101,435
5/12/03 10:35:20 AM
|
I'd probably never use it at all
Its a cool parlor trick I think. Thats about it.
"Packed like lemmings into shiny metal boxes. Contestants in a suicidal race." - Synchronicity II - The Police
|
Post #101,462
5/12/03 1:08:32 PM
|
Its a cool parlor trick...
...that has an annoying tendancy to show up on the [insert correct TLA here; the name of the test that you have to take to get into a Master's program] test.
(This gettin old just isn't what it's cracked up to be...)
jb4 "We continue to live in a world where all our know-how is locked into binary files in an unknown format. If our documents are our corporate memory, Microsoft still has us all condemned to Alzheimer's." Simon Phipps, SUN Microsystems
|
Post #101,466
5/12/03 1:25:21 PM
|
Not even really a C++ issue as such, is it?
You can do that evil thing he did in ANSI C with pointers. Have you seen this? \nregister n = (count + 7) / 8; /* count > 0 assumed */\n\n switch (count % 8)\n {\n case 0: do { *to = *from++;\n case 7: *to = *from++;\n case 6: *to = *from++;\n case 5: *to = *from++;\n case 4: *to = *from++;\n case 3: *to = *from++;\n case 2: *to = *from++;\n case 1: *to = *from++;\n } while (--n > 0);\n }\n Now THAT is evil!
-drl
|
Post #101,565
5/12/03 8:50:05 PM
|
Its a Duff Device
and as an optimization for moving chunks of data rapidly its pretty good.
"Packed like lemmings into shiny metal boxes. Contestants in a suicidal race." - Synchronicity II - The Police
|
Post #101,594
5/12/03 11:25:43 PM
|
Yep - it's assembler in C
-drl
|
Post #101,603
5/13/03 12:25:46 AM
|
Most ASM Languages have a SWAP instruction.
|
Post #101,651
5/13/03 11:18:05 AM
|
Yes, not my point
My point was that C is syntactically capable of these kinds of weird things you might use at a very low level.
-drl
|
Post #101,657
5/13/03 11:27:46 AM
|
And my point...
... (if there's really a point to be made) is that this is even lower level than assembly language (unless you count the One Instruction Machine). More like programming ASIC's. :-)
|
Post #101,654
5/13/03 11:20:14 AM
|
SWAP?
SWAP? Did you mean MOVB/MOVW?
-- -- Jim Weirich jweirich@one.net [link|http://w3.one.net/~jweirich|http://w3.one.net/~jweirich] --------------------------------------------------------------------- "Beware of bugs in the above code; I have only proved it correct, not tried it." -- Donald Knuth (in a memo to Peter van Emde Boas)
|
Post #101,853
5/14/03 1:45:11 PM
5/14/03 1:46:33 PM
|
That's ANSI C?!?
ANSI C compilers I've run across recently wouldn't allow you to put a case label inside of a block (i.e. inside of an open curly brace). I know ANSI C++ won't let you...
jb4 "We continue to live in a world where all our know-how is locked into binary files in an unknown format. If our documents are our corporate memory, Microsoft still has us all condemned to Alzheimer's." Simon Phipps, SUN Microsystems
Edited by jb4
May 14, 2003, 01:46:33 PM EDT
|
Post #101,854
5/14/03 1:56:36 PM
5/14/03 1:58:08 PM
|
That's ANSI C?!? ... YES
I just tried it undef GNU C++, GNU C and Solaris C++. Duff's device works fine under all of them.
(NOTE: I had to change "register n" to "register int n" for it to work in C++).
-- -- Jim Weirich jweirich@one.net [link|http://w3.one.net/~jweirich|http://w3.one.net/~jweirich] --------------------------------------------------------------------- "Beware of bugs in the above code; I have only proved it correct, not tried it." -- Donald Knuth (in a memo to Peter van Emde Boas)
Edited by JimWeirich
May 14, 2003, 01:58:08 PM EDT
|
Post #101,859
5/14/03 2:22:19 PM
|
Re: That's ANSI C?!?
No, it's antsy C!
Took me a while to figure out what the hell it was for the first time I saw it.
-drl
|