Linked by Thom Holwerda on Mon 24th Sep 2012 15:07 UTC, submitted by MOS6510
Thread beginning with comment 536431
To view parent comment, click here.
To read all comments associated with this story, please click here.
To view parent comment, click here.
To read all comments associated with this story, please click here.
Features
Linked by David Adams on 05/16/13 4:23 UTC
Linked by Thom Holwerda on 05/11/13 21:41 UTC
Linked by Thom Holwerda on 05/08/13 14:22 UTC
Linked by Thom Holwerda on 05/02/13 15:28 UTC
Linked by Thom Holwerda on 04/29/13 21:06 UTC
Linked by Thom Holwerda on 04/24/13 22:24 UTC
Linked by Thom Holwerda on 04/18/13 11:21 UTC
Linked by Thom Holwerda on 04/16/13 9:29 UTC
Linked by Thom Holwerda on 04/15/13 22:44 UTC
Linked by Thom Holwerda on 04/14/13 18:22 UTC, submitted by MOS6510
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.