Linked by Thom Holwerda on Mon 24th Sep 2012 15:07 UTC, submitted by MOS6510
General Development "I've been programming professionally for about 3 years at this point, and I've noticed some interesting patterns in other programmers I've worked with. One of the key differentiators among programmers is motivation. I'm not referring to an individual's passion to simply be successful in their career, but rather the type of work they want to pursue. The thing they want to do with computers every day, the types of problems they are interested in solving."
Permalink for comment 536431
To read all comments associated with this story, please click here.
RE: Sad Truth
by Alfman on Tue 25th Sep 2012 14:35 UTC in reply to "Sad Truth"
Member since:


"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.

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. ;)

Reply Parent Score: 2