Two common bit operation interview questions do not use the addition, subtraction, multiplication, division operator to calculate the sum of the two numbers and a = B * 3

Source: Internet
Author: User

Calculation of the sum of the two numbers and a = B * 3 without the addition, subtraction, multiplication, division, and Division Operator

Address: http://blog.csdn.net/morewindows/article/details/8720.37, thank you.

Welcome to Weibo: http://weibo.com/MoreWindows

 

The previous "bit operation basic article bit operation comprehensive summary" (http://blog.csdn.net/morewindows/article/details/7354571) introduced:

I. Base on bit operations, describe the application rules of bit operators in a table and explain them in detail.

2. Commonly Used bit operations tips, such as determining parity, exchanging two numbers, changing symbols, and calculating absolute values.

3. bitwise operations and space compression: space compression for sieve prime numbers.

4. Interesting applications of bit operations: lists four interesting applications: Bit operations in high-low-level switching, binary reverse order, number of 1 in binary, and missing numbers.

 

Now I will introduce two operation exercises. These two exercises are often used in the pen questions of major IT companies.

1. Do not use the addition, subtraction, multiplication, division operator to calculate the sum of two numbers

2. Do not use the addition, subtraction, multiplication, division operator to calculate a = B * 3

 

First, these two questions are actually very relevant and need to be calculated.

A = B * 3 is calculated as a = B * 2 + B, that is, a = B <1 + B.

Therefore, as long as the sum of the two digits is calculated without the addition, subtraction, multiplication, division, and a = B * 3 operators, the solution is solved. So how can we calculate the sum of two numbers without the addition, subtraction, multiplication, division, and so on? We know that when we calculate 5 + 9, if we do not carry 5 + 9 = 4, 5 + 9, The carry is 1, and then the sum is 1*10 + 4 = 14. This method can also be used in binary.

The procedure is as follows: // By morewindows (http://blog.csdn.net/MoreWindows)

With a = 3, B = 6.

A 0011

B 0110

Do not carry and 0101 = 5

Carry 0010 = 2

Therefore, A + B becomes 5 + 2 <1

Then there are

5 0101

2 <1 0100

Do not carry and 0001 = 1

Carry 0100 = 4

Therefore, A + B becomes 1 + 4 <1

Then there are

1 0001

4 <1 1000

Do not carry and 1001 = 9

Carry 0000 = 0

When carry is 0, do not carry is 9, that is, the sum of A + B.

 

The code is not hard to write:

// Two common bit operation questions without subtraction, multiplication, division operator to calculate the sum of the two and a = B * 3 // http://blog.csdn.net/morewindows/article/details/8710737//By morewindows (http://blog.csdn.net/MoreWindows) # include <stdio. h> # include <stdlib. h> # include <time. h> int bitadd (int A, int B) {int ncarry = A & B; // carry int nsumnocarry = a ^ B; // non-carry if (ncarry! = 0) return bitadd (nsumnocarry, ncarry <1); elsereturn nsumnocarry;} int bitmultiplication3 (int A) {return bitadd (A <1, );} int main () {printf ("two common bit operation interview questions do not use the addition, subtraction, multiplication, division operator to calculate the sum of the two numbers and a = B * 3 \ n "); printf ("-http://blog.csdn.net/morewindows/article/details/8710737-\ n"); printf ("-by morewindows (http://blog.csdn.net/MoreWindows-\ n"); srand (Time (null )); const int maxnumber = 100; int A = rand () % maxnumber, B = rand () % maxnumber; printf ("\ n ------------------ addition of bit operations -------------------- \ n "); printf ("% d + % d = % d \ n", a, B, bitadd (a, B )); printf ("\ n ------------------- bit operation A = B * 3 ------------------- \ n"); printf ("% d * 3 = % d \ n",, bitmultiplication3 (a); Return 0 ;}

The running result is as follows:


 

Calculation of the sum of the two numbers and a = B * 3 without the addition, subtraction, multiplication, division, and Division Operator

Address: http://blog.csdn.net/morewindows/article/details/8720.37, thank you.

Welcome to Weibo: http://weibo.com/MoreWindows

 

 

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.