C
[quote user="CodeRage"][quote user="Mikademus"][quote user="dicey"]On a related note, not too long ago on a developer's mailing list for a large open-source project that I read, the following C fragment was called "quite elegant" val -= (val>>31)|1;Wouldn't you agree? (it replaced the following code if you haven't figured it out by now (and who couldn't tell that the two are equivalent at a glance?))if (val > 0) val--;else val++;[/quote]
Mandatory reply follows
Use trigraphs for better representation of equivalence:
val > 0 ? --val : ++val;
End mandatory reply
[/quote]Yeah, val -= (val>>31)|1; seems quite "elegant" until someone needs to change the variable to a different bitsize.I would use. val += (val>0) ? 1 : -1;Using the bitshift just seems unportable, unreadable, unmaintainable, and just plain stupid. [/quote]Well, I assumed the previous post had the correct equivalent form. So, now that I remove this assumption, I would use... val += (val<0) ? 1 : -1;Which is more readable, and works better in case someone changes the int to a long, etc. Why oh why don't I ever test my posts here better ;)