-5
1
Write two functions/programs which take an ASCII code of character (8-bit unsigned integer) and one of them converts char to uppercase and other to lowercase.
That is, they must be equivalent to following functions:
unsigned char toUpper(unsigned char c){
if (c >= 97 && c <= 122) return c - 32;
return c;
}
unsigned char toLower(unsigned char c){
if (c >= 65 && c <= 90) return c + 32;
return c;
}
However you are not allowed to use branching (if
s, goto
s), loops and comparison and logical operators. Recursion, language built-in functions, regular expressions, arrays and objects also not allowed.
You may use only arithmetic and bitwise operations on 8-bit unsigned integers.
Score:
1 - for | & ^ ~ >> <<
operators
1.11 - for + -
operators
1.28 - for *
operator
1.411 - for / %
operators
Answer with lowest total score (for two functions added) wins. If answers with equal score than first (oldest) answer wins.
This good valid question deserves more upvotes. If you are an anti-micro-optimization kind of a guy, then you shouldn't even be at this question, let alone allowing your own biases and prejudices to cloud your judgement and downvote this legitimate question. Also, off-topicly, I would be very interested (fascinated) to see how you generated those score numbers. I'm not criticizing them, rather, just the opposite, I'm intrigued. Mabey you could comment a link to a resource about it? – Jack Giffin – 2017-08-10T16:13:25.830
@lolzerywowzery How I chose scores: At start I defined approximate scores I want operations should have, like 1, 1.1, 1.3 and 1.4. Then with trial and error I modified them a bit so equal score is unlikely with small operator amount. Checked that in Mathematica. – Somnium – 2017-08-10T18:55:50.773
Are regular expressions allowed? If yes, this is a duplicate of Converting a string to lower-case.
– ProgramFOX – 2014-08-19T13:24:54.2371@ProgramFOX No. – Somnium – 2014-08-19T13:25:53.233