Linked by Thom Holwerda on Mon 24th Sep 2012 15:07 UTC, submitted by MOS6510
Permalink for comment 536431
To read all comments associated with this story, please click here.
To read all comments associated with this story, please click here.
Features
Linked by Thom Holwerda on 06/13/13 14:35 UTC
Linked by Thom Holwerda on 06/11/13 17:07 UTC
Linked by Thom Holwerda on 06/10/13 23:13 UTC
Linked by Thom Holwerda on 06/08/13 14:57 UTC
Linked by Thom Holwerda on 06/07/13 11:40 UTC
Linked by Thom Holwerda on 06/04/13 12:45 UTC
Linked by nfeske on 05/31/13 10:12 UTC
Linked by Thom Holwerda on 05/29/13 16:59 UTC
Linked by Thom Holwerda on 05/24/13 17:26 UTC
Linked by Thom Holwerda on 05/21/13 21:38 UTC
More Features »
Sponsored Links



Member since:
2011-01-28
JeeperMate,

"int myAbs(const int num) { return num < 0 ? (~num) + 1 : num; }"
I concur with everyone else that taking the not and adding one is a roundabout way of calculating what we really want, which is a simple negation.
However I'll go one further and claim that your solution is potentially invalid because you make an assumption about two's compliment that C doesn't guarantee. In fact it explicitly lists 3 different number sign representations.
http://stackoverflow.com/questions/3952123/representation-of-negati...
Now I'm clearly playing devil's advocate here, but arguably an ABS function implementation that converts an integer to string and removes the sign character is, according to the C standard, more correct than one that makes assumptions about the bits.
All this time you were laughing at him, and yet his inefficient solution may have been more correct than yours. I hope you can laugh at the irony.