# 加减乘除的位运算封装

### 简单实现的加减乘除位运算：

#include <iostream>

#include <algorithm>
#include <stdio.h>
#include <stdlib.h>
#include <cmath>
#include <cstring>
using namespace std;
//--------------------------------加
if(b == 0)
return a;
int s = a ^ b;
int c = (a&b)<<1;
}

int s;
int c;
while(b != 0){
s = a ^ b;
c = (a & b)<<1;
a = s;
b = c;
}
return s;
}
//-------------------------------减
int contrast(int i){
}
int subtraction(int a,int b){
}
//-------------------------------乘
int getsign(int i){
return (i>>31);
}
int bepositive(int i){
if(i>>31)
contrast(i);
else
return i;
}

int multiplay_1(int a,int b){
bool flag = true;
if(getsign(a) == getsign(b)){
flag = false;
}
a = bepositive(a);
b = bepositive(b);
int sum = 0;
while(b){
b = subtraction(b,1);
}
if(flag) return contrast(sum);
else return sum;
}

int multiplay_2(int a,int b){
bool flag = true;
if(getsign(a) == getsign(b)){
flag = false;
}
a = bepositive(a);
b = bepositive(b);
int sum = 0;
while(b){
if(b & 1){
}
a = a<<1;
b = b>>1;
}
if(flag) return contrast(sum);
else return sum;
}
//-------------------------------除
int division_1(int a,int b){
if(b == 0){
return 0;
}
bool flag = true;
if(getsign(a) == getsign(b)){
flag = false;
}
a = bepositive(a);
b = bepositive(b);
int sum = 0;
a = subtraction(a,b);
while(a>=0){
a = subtraction(a,b);
}
if(flag) return contrast(sum);
else return sum;
}

int division_2(int a,int b){
if(b == 0){
return 0;
}
bool flag = true;
if(getsign(a) == getsign(b)){
flag = false;
}
a = bepositive(a);
b = bepositive(b);
int sum = 0;
int i = 31;
while(i >= 0){
if((a >> i) >= b) {
a = subtraction(a,b<<i);
}
i = subtraction(i,1);
}
if(flag) return contrast(sum);
else return sum;
}

int main(){
cout<<subtraction(4,7)<<endl;
cout<<multiplay_1(4,7)<<endl;
cout<<multiplay_2(4,7)<<endl;
cout<<division_1(40,7)<<endl;
cout<<division_2(40,7)<<endl;

return 0;
}

• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120