Search code examples
cbitunsigned-integer

Add() method won't return correct result


So I have to make an add method that will take two 16 bit integers and return that result without using arithmetic operators. Unfortunately when inputting two numbers like 5 and 10, it returns 0 instead of 15. Is there anything I'm doing wrong in my code? The code is given below and is done in C

addition_subtraction_result add(uint16_t augend, uint16_t addend) {
    addition_subtraction_result addition;
    while (addend != 0){
      int carry = augend & addend;
      augend = augend ^ addend;
      addend = carry << 1;
    }
    addition.overflow = false;
    return addition;
}

Let me know if you need more information. Thanks!


Solution

  • You return addition (hopefully by value) and the only thing you set is addition.overflow = false;