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:

  • Does Background Music Matter to Speech in Language Models
  • Measuring the Checker
  • Mastermind
  • Resillience
  • Multi-Head Attention