Bit arithmetic - addition, subtraction achieve

Basic properties: 1: ~ n = - (n + 1), for example: 1-3 = -4
        2: Get the last one integer n binary string: -n & n = ~ (n-1) & n
        3: remove integer n binary string last 1: n & (n-1

Addition: (All of the following code is implemented in Java)

?
1
2
3
4
5
6
7
8
9
10
11
public static int add( int a, int b) {
       int res=a;
       int xor=a^b;  // a^b得到原位和(相当于按位相加没有进位)
       int forward=(a&b)<< 1 ; //得到进位和   a&b:得到产生进位的地方 (a&b)<<1:进位后的值
       if (forward!= 0 ){ //若进位和不为0,则递归求原位和+进位和
           res=add(xor, forward);
       } else {
           res=xor; //若进位和为0,则此时原位和为所求和
       }
       return res;               
   }

Subtraction:

?
1
2
3
4
public static int minus( int a, int b) {
       int B=~(b- 1 );  // 由上面基本性质   -b=+(-b),~(b-1)=-b  ===>>>  a-b=a+(-b)=a+(~(b-1)
       return add(a, B);       
   }

multiplication:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public static  int multi( int a, int b){
       /*     1011
           *  1010
          --------
             10110 (1011<<1,相当于乘以0010)
           1011000 (1011<<3,相当于乘以1000)
           --------
           1101110
       */
       int i= 0 ;
       int res= 0 ;
       while (b!= 0 ){ //乘数为0则结束
           //处理乘数当前位
           if ((b& 1 )== 1 ){
               res+=(a<<i);
               b=b>> 1 ;
               ++i; //i记录当前位是第几位
           } else {
               b=b>> 1 ;
               ++i;
           }
       }
       return res;
   }

division:

?
1
2
3
4
5
6
7
8
9
10
public static  int sub( int a, int b) {
       // 除法的意义就在于:求a可以由多少个b组成。那么由此我们可得除法的实现:求a能减去多少个b,做减法的次数就是除法的商。
       int res=- 1 ;
       if (a<b){
           return 0 ;
       } else {
           res=sub(minus(a, b), b)+ 1 ;
       }
       return res;
   }

Test code:

?
1
2
3
4
5
6
public static void main(String args[]){
       System.out.println( "加法测试:" +add( 10 , 5 ));
       System.out.println( "减法测试:" +minus( 10 , 5 ));
       System.out.println( "乘法测试:" +multi( 10 , 5 ));
       System.out.println( "除法测试:" +sub( 10 , 5 ));
   }

Test Results:

  

 

 

Source: https://www.cnblogs.com/xiaoyh/p/10251731.html

Basic properties: 1: ~ n = - (n + 1), for example: 1-3 = -4
        2: Get the last one integer n binary string: -n & n = ~ (n-1) & n
        3: remove integer n binary string last 1: n & (n-1

Addition: (All of the following code is implemented in Java)

?
1
2
3
4
5
6
7
8
9
10
11
public static int add( int a, int b) {
       int res=a;
       int xor=a^b;  // a^b得到原位和(相当于按位相加没有进位)
       int forward=(a&b)<< 1 ; //得到进位和   a&b:得到产生进位的地方 (a&b)<<1:进位后的值
       if (forward!= 0 ){ //若进位和不为0,则递归求原位和+进位和
           res=add(xor, forward);
       } else {
           res=xor; //若进位和为0,则此时原位和为所求和
       }
       return res;               
   }

减法:

?
1
2
3
4
public static int minus( int a, int b) {
       int B=~(b- 1 );  // 由上面基本性质   -b=+(-b),~(b-1)=-b  ===>>>  a-b=a+(-b)=a+(~(b-1)
       return add(a, B);       
   }

乘法:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public static  int multi( int a, int b){
       /*     1011
           *  1010
          --------
             10110 (1011<<1,相当于乘以0010)
           1011000 (1011<<3,相当于乘以1000)
           --------
           1101110
       */
       int i= 0 ;
       int res= 0 ;
       while (b!= 0 ){ //乘数为0则结束
           //处理乘数当前位
           if ((b& 1 )== 1 ){
               res+=(a<<i);
               b=b>> 1 ;
               ++i; //i记录当前位是第几位
           } else {
               b=b>> 1 ;
               ++i;
           }
       }
       return res;
   }

除法:

?
1
2
3
4
5
6
7
8
9
10
public static  int sub( int a, int b) {
       // 除法的意义就在于:求a可以由多少个b组成。那么由此我们可得除法的实现:求a能减去多少个b,做减法的次数就是除法的商。
       int res=- 1 ;
       if (a<b){
           return 0 ;
       } else {
           res=sub(minus(a, b), b)+ 1 ;
       }
       return res;
   }

测试代码:

?
1
2
3
4
5
6
public static void main(String args[]){
       System.out.println( "加法测试:" +add( 10 , 5 ));
       System.out.println( "减法测试:" +minus( 10 , 5 ));
       System.out.println( "乘法测试:" +multi( 10 , 5 ));
       System.out.println( "除法测试:" +sub( 10 , 5 ));
   }

测试结果:

  

Guess you like

Origin www.cnblogs.com/mq0036/p/12153531.html