The Elements of Programming Interviews book has a question on computing x*y without arithmetical operators (question 5.5). The solution is here: https://github.com/epibook/epibook.github.io/blob/master/solutions/cpp/MultiplyShiftAdd.ccI am confused about the Add() function in the solution. For example, if I want to add a=1110 and b=1, the while loop will execute only once (because temp_b will decrease from 1 to 0). The iteration goes as follows:ak = 1110 & 1 = 0, bk = 1 & 1 = 1,carryout = (0 & 1) | 0 | 0 = 0,sum = sum | (ak ^ bk ^ carryin) = 0 | 1 = 1,carryin = carryout << 1 = 0,k = 10,temp_a = 111,temp_b = 0Then we return:sum | carryin = 1But the sum of 1110 and 1 should be 1111EDIT: I realised that the while loop would not stop after the first iteration
EPI question on multiplying two integers
algorithms;arithmetic;multiplication