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

#include <iostream>

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

int add_2(int a,int b){
    int s;
    int c;
    while(b != 0){
       s = a ^ b;
       c = (a & b)<<1;
       a = s;
       b = c;
    }
    return s;
}
//-------------------------------减
int contrast(int i){
    return add_1(~i,1);
}
int subtraction(int a,int b){
    return add_1(a,contrast(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){
        sum = add_1(sum,a);
        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){
            sum = add_1(sum,a);
        }
        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){
        sum = add_1(sum,1);
        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) {
           sum = add_1(sum,1<<i);
           a = subtraction(a,b<<i);
        }
        i = subtraction(i,1);
    }
    if(flag) return contrast(sum);
    else return sum;
}


int main(){
    cout<<add_1(4,7)<<endl;
    cout<<add_2(4,7)<<endl;
    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;
}