Bitwise Operations implement addition, subtraction, multiplication, division, and four arithmetic operations

Source: Internet
Author: User

Bitwise Operations implement addition, subtraction, multiplication, division, and four arithmetic operations
1. Description

How do I use bitwise operations to perform the addition, subtraction, multiplication, and division of integers?

2. Solution
You need to be familiar with the bit operation implementation of some common functions, specifically:

<1> common equations:-n = ~ (N-1) = ~ N + 1

<2> obtain the last 1: n & (-n) or n & ~ in the binary value of integer n &~ (N-1), for example: n = 010100,-n = 101100, n & (-n) = 000100

<3> remove the last 1: n & (n-1) in the binary value of integer n, for example, n = 010100, n-1 = 010011, n & (n-1) = 010000.

(1) Addition implementation

It is easy to use the "XOR" and "or" operations to perform integer addition: the "XOR" Operation of the corresponding number of digits can obtain the value of this digit, the "and operation" of the corresponding bit can obtain the high carry produced by this bit, for example, a = 010010, B = 100111. The calculation steps are as follows:

First round: a ^ B = 110101, (a & B) <1 = 000100, because the carry (000100) is greater than 0, it enters the next round of calculation, a = 110101, B = 000100, a ^ B = 110001, (a & B) <1 = 001000, because the carry value is greater than 0, it enters the next round of calculation: a = 110001, B = 001000, a ^ B = 111001, (a & B) <1 = 0, carry is 0, termination, Calculation Result: 111001.

The Code is as follows:

Int add (int a, int B ){
Int carry, add;
Do {
Add = a ^ B;
Carry = (a & B) <1;
A = add;
B = carry;
} While (carry! = 0 );
Return add;
}

(2) Subtraction

Subtraction can be easily converted into addition: a-B = a + (-B) = a + (~ B + 1)

The Code is as follows:

Int subtract (int a, int B ){
Return add (a, add (~ B, 1 ));
}

(3) Multiplication

First look at an instance:

1011*1010: 1011
* 1010
----------
10110 <shifts one digit to the left, multiplied by 0010
+ 1011000 <move three places left, multiplied by 1000
----------
1101110 Multiplication can be completed through the series shift and addition methods. The last 1 can be passed through B &~ (B-1) obtained, can be removed through B & (b-1), in order to efficiently get the number of digits left shift, you can calculate a map in advance, the Code is as follows: int multiply (int a, int B) {
Bool neg = (B <0 );
If (B <0)
B =-B;
Int sum = 0;
Map Bit_map;
For (int I = 0; I <32; I ++)
Bit_map.insert (pair (1 <I, I ));
While (B> 0 ){
Int last_bit = bit_map [B &~ (B-1)];
Sum + = (a <last_bit );
B & = B-1;
}
If (neg)
Sum =-sum;
Return sum;
}

(4) Division implementation

Multiplication can be easily converted to subtraction. The main idea is similar to multiplication. The Code is as follows: int divide (int a, int B ){
Bool neg = (a> 0) ^ (B> 0 );
If (a <0)
A =-;
If (B <0)
B =-B;
If (a <B)
Return 0;
Int msb = 0;
For (msb = 0; msb <32; msb ++ ){
If (B <msb)> =)
Break;
}
Int q = 0;
For (int I = msb; I> = 0; I --){
If (B <I)>)
Continue;
Q | = (1 <I );
A-= (B <I );
}
If (neg)
Return-q;
Return q;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.