求连两个整数平均值的n种境界

1,(a+b)2

#include<stdio.h>
#include<windows.h>
int main()
{
    int a = 3;
    int b = 7;
    int c = 0;
    c = (a + b) / 2;
    printf("%d",c);
    system("pause");
    return 0;
}

2,(a-b)/2

#include<stdio.h>
#include<windows.h>
int main()
{
    int a = 3;
    int b = 7;
    int c = 0;
    c = a + (b-a) / 2;
    system("pause");
        return 0;
}

3,位运算

#include<stdio.h>
#include<windows.h>
int main()
{
    int a = 3;
    int b = 7;
    int c = 0;
    c = a + ((b - a)>>1);
    system("pause");
    return 0;
}

4,位运算

#include<windows.h>
int main()
{
    int a = 3;
    int b = 7;
    int c = 0;
    c = (a&b) + ((a^b) >> 1);//a&b的结果相当于把a和b中相同的二进制位除2后的结果,而a^b的结果相当于a和b中不同的二进制位拿下来。
    system("pause");
    return 0;
}

猜你喜欢

转载自blog.csdn.net/aixintianshideshouhu/article/details/81282532