> There are three methods that produce same result value.
> Case of 1
> char a = 18, b = 17, result;
> a = a >> 4; b = b >> 4;
> result = a - b;
> Case of 2
> char a = 18, b = 17, result;
> a = a & 0xF0;
> b = b & 0xF0;
> result = a - b;
> Case of 3
> char a = 18, b = 17, result;
> a = a - 15;
> b = b - 15;
> result = a - b;
standards compliant C compiler. Did you actually compile and run your
examples? If so, and they produced the same output, I would suggest using a
different system. Place the following in a file, compile it, and run it:
#include <stdio.h>
int main() {
char a, b, result;
a = 18, b = 17;
a = a >> 4; b = b >> 4;
result = a - b;
printf( "%d\n", result );
a = 18, b = 17;
a = a & 0xF0;
b = b & 0xF0;
result = a - b;
printf( "%d\n", result );
a = 18, b = 17;
a = a - 15;
b = b - 15;
result = a - b;
printf( "%d\n", result );
You should get three lines of output:Quote:}
0
0
1
Even assuming all three variants produced the same result the answer to yourQuote:> Can anyone tell me which is faster and effective? And what's the reason?
question is: It depends. It depends on your CPU architecture. It depends on
your compiler. It depends on the compilation options you use.