#include<stdio.h>
int main(void){
int a = 10;
int b = 5;
int num = 0;
if (a > b){
num = b + (a - b) / 2;
}
else{
num = a + (b - a) / 2;
}
printf("%d", num);
return 0;
}
除了以上这种比较丑陋的解决方案外,我们还要一种更为优雅、从容的方案,那就是用位移操作符进行运算
#include<stdio.h>
int main(void){
int a = 10;
int b = 5;
printf("%d\n", (a&b) + ((a^b) >> 1));
printf("%d\n", (b + ((a - b) >> 1)));
return 0;
}