Example: Find the sum of two numbers without using or ‐ - rFronteddu/general_wiki GitHub Wiki

  • AND gives the location of the carry
  • XOR gives us the sum without carry
  • SHIFT moves the carry to be added at the next cycle
int add(int a, int b) {
    while (b != 0) {
        int carry = a & b;  // Identifies positions where carry is generated.
        a = a ^ b;  // Adds without considering carry.
        b = carry << 1;  // Moves carry left to be added in the next iteration.
    }
    return a;
}