# Time complexity of binary sum

What is the time complexity of binary sum, the sum of two binary numbers done like in elementary school?

Say one number is F and his length is $s$ bits, and another number is H and his length is $t$. (Like Time complexity of binary multiplication? , but with sum).

• Welcome to Math.SE! Does "binary sum" mean you are adding numbers represented in base two, or does it mean you are adding two numbers? Or both? It would be helpful if you could add some context to the Question, not least because it would help Answers be written at a level that is useful to you. – hardmath Dec 2 '13 at 11:37
• @hardmath binary sum mean you are adding numbers represented in base two – ben Dec 2 '13 at 11:39

The short answer is that adding two numbers by the "elementary school" algorithm has linear complexity. That is, given binary representations F and H of respective lengths $s$ and $t$, the number of steps needed is $O(s+t)$.
This should be intuitively clear. After arranging the longer number over the shorter one, starting at the right hand side and adding-with-carry from right to left will generate a sum of length at most $\max(s,t)+1$. The computation required for each bit of the result is $O(1)$ or constant, since this merely combines the two respective bits of F and H with a possible carry from the previous step. (There are at most eight possibilities for such a step.)
• Yes, which is roughly what the earlier Question you linked to says. We might think in terms of doing the multiplication as shift-and-add, based on the bits of the shorter multiplicand, so that you need to add (at most) $s$ shifted copies of the $t$ bit length number, which amounts to $(s-1)$ additions of (at most) $s+t-1$ bit length representations. I leave it to you to verify $s*O(s+t) = O(s*t)$ when $s \le t$. – hardmath Dec 2 '13 at 13:36
• In that Question $n=s=t$, and we are just taking the large size to fit both multiplicands. The Accepted Answer there mentions using two different sizes, one of $n$ bits and one of $m$ bits. But these are exercises for you to think through! – hardmath Dec 2 '13 at 13:59