#include <stdio.h>
int average(int a, int b){
/*
* (a&b) is the same part of number a and b, (a^b)>>1
* is half of the diference part of number a and b.
* bit computing is faster than multiplication and divsion
*/
return (a&b) + ((a^b)>>1);
}
int main(void)
{
printf("(783 + 217)/2 = %d\n",average(783,217));
return 0;
}
快速高效的求两个整数的平均值(忽略小数)
最新推荐文章于 2022-10-26 17:11:45 发布