The first version would get you tarred and feathered in a code review. Never thought of doing the latter with an and, it's normally done with subtraction.
The first method is bullet proof and portable. It should work on any compiler. The second method should work but I am not set up to run it.
My largest project was around 10,000 lines of code. Isuues of mainyainablity and readability matter.
For someone later on getting into the code the first method would be obvious to read and understand. I new of situations when people left a company code had to be compely rewritten. I had someone under me on a project who left the company and I had to rewrite from scratch.
If the problem is readability, then surely the solution is comments?
For example:
Code:/** * Converts a digit character (0 to 9) to an integer. Assumes ASCII or Unicode encoding. * * @return int the integer represented by the given character. Returns -1 if the character is not a digit */ int text2int(char text_digit) { // If the given char is actually a digit... if(text_digit >= '0' && text_digit <= '9') // ...then use a bitmask to get its value as an integer and return it as an int. // @see http://www.asciitable.com return text_digit & 0x0f; return -1; }
Disclaimer: I'm not competent in C.
I rely on open-source software to build websites, so I have to read a lot of other people's code. I actually do far more code-reading than code-writing. If the comments tell me what an algorithm is doing at each statement or block, I can understand it what it does without immediately understanding the code's logic.
Comments also help people find bugs, because it makes it easy to see when a algorithm doesn't do what the author says it does. I've had to bughunt in code I wrote years ago and I don't remember what I was thinking when I wrote it.
Actually, in most cases I'm opposed to comments. The problem is they don't always accurately describe what's going on. I much prefer making the names in the code so clear there's no need of a comment--I actually regard a comment describing what's happening as a bit of a code smell. Comments describing something external to the code are another matter, I'm quite willing to use them then.