Increment a number without using arithmetic operators

All I want to do is increment the i++ at the end of the for loop without using arithmetic operators and I can only use bitwise operators. So no "+, ++, +=, -, --, -= ,∗, /, %". An exception is that I can use addition for pointer arithmetic like in the line

result += multiplicand;

Would I have to change it into a while loop or how would I do that?

int result = 0;
int i = 0;
for (i=0; i<multiplier; i++){
      result += multiplicand;
    }

1 answer

  • answered 2021-03-03 02:00 Antonin GAVREL

    What you are looking for is Shift-and-Add Multiplication:

    Shift-and-add multiplication is similar to the multiplication performed by paper and pencil. This method adds the multiplicand X to itself Y times, where Y denotes the multiplier. To multiply two numbers by paper and pencil, the algorithm is to take the digits of the multiplier one at a time from right to left, multiplying the multiplicand by a single digit of the multiplier and placing the intermediate product in the appropriate positions to the left of the earlier results.

    Give it a try and let me know if you are still lost