You probably can, but it is very difficult to understand.
XOR: a byte value with a constant is not faster than adding (or subtracting) a constant. And the advantage that it becomes a switch (i.e. toupper() and tolower() can be the same code) is very small, because the amount of code is so small.
When disassembling, these two functions:
int my_tolower1(int c) { return c + 'a' - 'A'; } int my_tolower2(int c) { return c ^ ('a' - 'A'); }
Pretty much compiles to the same, modulo, of course, adds vs xor:
my_tolower1(int): pushq %rbp movq %rsp, %rbp movl %edi, -4(%rbp) movl -4(%rbp), %eax addl $32, %eax popq %rbp ret my_tolower2(int): pushq %rbp movq %rsp, %rbp movl %edi, -4(%rbp) movl -4(%rbp), %eax xorl $32, %eax popq %rbp ret
Both addl and xorl have three bytes, so there are no differences. I assume that both of them are the same cycles on the most interesting processors these days.
Please note that, as I said in my comment, you should not go around and assume that your C program runs in an environment where you can make such assumptions. The Linux kernel, however, is such an environment.
source share