Alternative additions

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/iov3Rain/article/details/90244326

Title Description

Please write a function to add two numbers. Not use + or other arithmetic operators.

Given two int A and B. Please return to a value of A + B

Test sample:

1,2

Returns: 3

 

 

Binary operation

A representative of a carry

B Representative Results

Make a binary feel the draft

 

class UnusualAdd {
public:
    int addAB(int A, int B) {
        // write code here
        while(A)
        {
            int temp = B;
            B = A ^ B;
            A = A & temp;
            A <<= 1;
        }
        return B;
    }
};

Guess you like

Origin blog.csdn.net/iov3Rain/article/details/90244326