47-Bitwise implementation Subtraction arithmetic

Source: Internet
Author: User

Implement two-digit arithmetic without using the +-*/arithmetic character.

1. Add

The addition of two numbers is achieved with bits. The addition of 2 numbers and the overflow problem are not considered.
such as 9+15=24
1001 + 1111, due to binary 0+0=0,1+0=1, 0+1=1, 1+1=0, can be found to be XOR operation, and produce carry, then only 1, 1 add, that is, with the operation.

int add(int nums1, int nums2) { if (nums1 == 0 || nums2 == 0) return (nums1 == 0) ? nums2 : nums1; int sum = 0, carry = 0; do { sum = nums1 ^ nums2; carry = (nums1 & nums2) << 1; // 进位左移一位 nums1 = sum; nums2 = carry; } while (nums2); return sum;}
2. Reduce

A-B = A + (-a), which requires only a negative operation, that is, complement: bitwise negation + 1

int negtive(int num) {    return1);}intsub(int nums1, int nums2) {    return add(nums1, negtive(nums2));}
3. Multiply

such as multiplication in decimal, bitwise multiply.
To find the product of 2 positive integers

int multi(int nums1, int nums2) { if (nums1 == 0 || nums2 == 0) return 0; int result = 0; while (nums2) { if (nums2 & 0x01) { // 乘数的最后1位 result = add(result, nums1); } nums1 <<= 1; // 被乘数左移1位 nums2 >>= 1; // 取乘数的下一位 } return result;}
4. In addition to

A quotient that divides two positive integers.
Multiply the inverse.
Division is the inverse of the multiplication process, X/y process: × minus (if x is reduced) y^ (2^31), y^ (2^30),... y^8,y^4,y^2,y^1. Subtract the corresponding number of y in the result plus the corresponding quantity.

intDivision (intNUMS1,intNUMS2) {if(Nums1 = =0|| NUMS2 = =0)return 0;Const intBITS =sizeof(NUMS1) *8;intresult =0; for(inti = bits-1; I >=0; i--) {//The reason for not using nums2 << I compared with nums1 is to prevent overflow        if((nums1 >> i) > Nums2) {result = Add (result,1<< i);        Nums1 = Sub (nums1, nums2 << i); }    }returnResult;}

Reference: http://blog.csdn.net/hackbuteer1/article/details/7390093

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

47-Bitwise implementation Subtraction arithmetic

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.