How to add two numbers without add()

I’m going to simulate the whole adding process by bit manipulation:

def add(sum, carry):
    if not carry:
        return sum

    sum = sum ^ carry           # Just add without carry
    carry = (sum & carry) << 1  # Just carry without add

    add(sum, carry)

a,b = 1,1
result = add(a,b)

and it works 🙂




Enjoy Reading This Article?

Here are some more articles you might like to read next:

  • Resillience
  • Multi-Head Attention
  • Preference Alignment 101
  • Challenges in Code Generation
  • PREDICTING AND OPTIMIZING LLVM COMPILER PASS ORDER