PracticeDev/study_clang/BitArithmetic/main.cpp

210 lines
5.8 KiB
C++
Raw Normal View History

2022-12-20 17:31:11 +08:00
/*************************************************************************
> File Name: main.c
> Author: TL Song
> Mail: songtianlun@frytea.com
> Created Time: Thu 27 Aug 2020 12:08:59 PM UTC
************************************************************************/
#include<stdio.h>
#include<time.h>
#include<map>
#include<iostream>
using namespace std;
int bitAdd(int num1, int num2)
{
int exclusive_val = num1; //异或值
int carry_val = num2; //进位值
exclusive_val = num1 ^ num2; //取第一次异或值(没有进位的异或就是相加)
carry_val = (num1 & num2) << 1; //取第一次进位值
//进位值为0是循环终止条件
while (carry_val)
{
int temp = exclusive_val;
exclusive_val = exclusive_val ^ carry_val; //异或值与进位值进行异或
carry_val = (temp & carry_val) << 1;
}
return exclusive_val;
}
int add(int a, int b)
{
return a+b;
}
int bitSubtraction(int num1, int num2)
{
int temp = bitAdd(~num2, 1); //取反与求补差1
return bitAdd(num1, temp);
}
int subtraction(int a, int b)
{
return a - b;
}
int bitDivide(int x, int y) {
int ans=0;
for(int i=31;i>=0;i--)
{
// //比较x是否大于y的(1<<i)次方避免将x与(y<<i)比较因为不确定y的(1<<i)次方是否溢出
if( (x>>i) >=y )
{
ans+=(1<<i);
x-= (y<<i);
}
}
return ans;
}
int divide(int a, int b)
{
return a/b;
}
int multiply(int a, int b)
{
return a * b;
}
int bitMultiply(int a, int b)
{
int ans = 0;
while(b)
{
if(b&1) //b最后一位是否为1
ans = bitAdd(ans, a);
a = (a<<1);
b = (b>>1);
}
return ans;
}
int mulTwo(int n)
{
return n << 1;
}
int divTwo(int n)
{
return n >> 1;
}
int getAverage(int x, int y)
{
return ( x + y ) >> 1;
}
int main()
{
int i;
int a,b;
int result;
int num = 10000000;
clock_t start, finish;
double duration;
//printf("Test Add:\n");
printf("Please Enter the num1: ");
scanf("%d",&a);
printf("Please Enter the num2: ");
scanf("%d",&b);
printf("---------------------------------------------------------------------------------------------\n");
start = clock();
for(i=0;i<num;i++)
result = bitAdd(a,b);
// printf("Bit add calculation result: %d, ", bitAdd(a,b));
finish = clock();
duration = (double)(finish - start) / CLOCKS_PER_SEC;
printf("> Bit add calculation %d times total time: %f, Last result: %d\n", num,duration,result);
start = clock();
for(i=0;i<num;i++)
result = add(a,b);
// printf("Direct add calculation result: %d, ", (a+b));
finish = clock();
duration = (double)(finish - start) / CLOCKS_PER_SEC;
printf("> Direct add calculation %d times total time: %f, Last result: %d\n", num,duration,result);
printf("---------------------------------------------------------------------------------------------\n");
start = clock();
for(i=0;i<num;i++)
result =bitMultiply(a,b);
finish = clock();
duration = (double)(finish - start) / CLOCKS_PER_SEC;
printf("> Bit multiply calculation %d times total time: %f, Last result: %d\n", num,duration,result);
start = clock();
for(i=0;i<num;i++)
result = multiply(a,b);
finish = clock();
duration = (double)(finish - start) / CLOCKS_PER_SEC;
printf("> Direct multiply calculation %d times total time: %f, Last result: %d\n", num,duration,result);
printf("---------------------------------------------------------------------------------------------\n");
start = clock();
for(i=0;i<num;i++)
result =bitDivide(a,b);
finish = clock();
duration = (double)(finish - start) / CLOCKS_PER_SEC;
printf("> Bit divide calculation %d times total time: %f, Last result: %d\n", num,duration,result);
start = clock();
for(i=0;i<num;i++)
result = divide(a,b);
finish = clock();
duration = (double)(finish - start) / CLOCKS_PER_SEC;
printf("> Direct divide calculation %d times total time: %f, Last result: %d\n", num,duration,result);
printf("---------------------------------------------------------------------------------------------\n");
start = clock();
for(i=0;i<num;i++)
result = mulTwo(a);
finish = clock();
duration = (double)(finish - start) / CLOCKS_PER_SEC;
printf("> Bit multiply 2 calculation %d times total time: %f, Last result: %d\n", num,duration,result);
start = clock();
for(i=0;i<num;i++)
result = multiply(a,2);
finish = clock();
duration = (double)(finish - start) / CLOCKS_PER_SEC;
printf("> Direct multiply 2 calculation %d times total time: %f, Last result: %d\n", num,duration,result);
printf("---------------------------------------------------------------------------------------------\n");
start = clock();
for(i=0;i<num;i++)
result = divTwo(a);
finish = clock();
duration = (double)(finish - start) / CLOCKS_PER_SEC;
printf("> Bit divide 2 calculation %d times total time: %f, Last result: %d\n", num,duration,result);
start = clock();
for(i=0;i<num;i++)
result = divide(a,2);
finish = clock();
duration = (double)(finish - start) / CLOCKS_PER_SEC;
printf("> Direct divide 2 calculation %d times total time: %f, Last result: %d\n", num,duration,result);
printf("---------------------------------------------------------------------------------------------\n");
start = clock();
for(i=0;i<num;i++)
result = getAverage(a,b);
finish = clock();
duration = (double)(finish - start) / CLOCKS_PER_SEC;
printf("> Bit average calculation %d times total time: %f, Last result: %d\n", num,duration,result);
start = clock();
for(i=0;i<num;i++)
result = divide(a+b,2);
finish = clock();
duration = (double)(finish - start) / CLOCKS_PER_SEC;
printf("> Direct average calculation %d times total time: %f, Last result: %d\n", num,duration,result);
printf("---------------------------------------------------------------------------------------------\n");
}