# How to show carries in long addition?

I am doing binary addition in LaTeX, and I cannot figure out how to show carried numbers above my work. (That is, 1 + 1 = 0 with carry 1, etc.)

Here is a minimal example of my addition without carries:

$\begin{array}[t]{r} 1100\ 1001 \\ + \ 1111\ 1111 \\ \hline 1100\ 1000 \end{array}$ \\

How can I show my carried numbers above the top line (in a smaller typeface)?

-
You could try \overset{1}{1}. – Qrrbrbirlbel Jan 29 '13 at 4:19
Oversetting the characters worked perfectly. Thank you! – user41419 Jan 29 '13 at 4:34
Have you checked out the polynom package? It has some very nice features. – A.Ellett Jan 29 '13 at 4:35
Take a look at the package ctan.org/pkg/xlop . See the page 12 of the documentation. – projetmbc Feb 2 '13 at 23:44

Do not use inline math ($…$) for big arrays. I’d use displayed math ($…$). If you are using the amsmath package (which is required for \overset and always a good idea), the unnumbered equation* environment may be used.

## \carry macro

The \carry[<num>] macro oversets the following group with <num> or 1 if the optional argument is not used.

## B column type

I also introduced a new column type with the help of the array package, that automatically repeats the right amount of r columns and inserts a appropriate horizontal space (that can be universally changed). If more than nine columns are needed the number has to be grouped: B{12}.

## Code

\documentclass{article}
\usepackage{array,mathtools}
\newcommand*{\carry}[1][1]{\overset{#1}}
\newcolumntype{B}[1]{r*{#1}{@{\,}r}}
\begin{document}
\begin{equation*}
\begin{array}{B3}
\carry 0 & \carry 1\carry 1\carry 0\carry 0 & \carry 1\carry 0\carry 01 \\
{} + 0 &                             1111 &                      1111 \\ \hline
1 &                             1100 &                      1000 \\
\end{array}
\end{equation*}

\begin{equation*}
\begin{array}{@{}B2}
\carry 2\carry 5 & \carry 4\carry[2] 68 \\
24 &                  389 \\
{} + 31 &                  436 \\ \hline
81 &                  293 \\
\end{array}
\end{equation*}
\end{document}

## Output

-
@PeterGrill Yes, you are right. Though I wanted to re-create the output of {} + <number> without the extra horizontal space on the left side (and without a longer \hline), with \mathbin the empty group is still needed. As \mathrel does not produce the right spacing I have changed it to {} + which is more common anyway. One might prepend the column specifications with @{} to get a more pleasing output (as I did in my updated second example). – Qrrbrbirlbel Feb 2 '13 at 22:50

Every year I teach a introductory course on computer architecture and I always begin the course with a review of decimal arithmetic to demonstrate the general principles of radix representation and arithmetic.

I also wanted to represent the carry in binary addition but my solution uses a Tikz matrix of math nodes. A minimal example below shows my solution.

\documentclass{article}

\usepackage{lmodern}
\renewcommand*\familydefault{\sfdefault}

\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{matrix}

Calculate the binary sum $0001\;0011\;1101+0000\;1011\;0111$.
$$\begin{tikzpicture}[ row 1/.style={font=\textsl,font=\scriptsize,black!85, anchor=west, inner sep=1.5pt}, every node/.style={column sep=.5mm,row sep=1mm}] \matrix (m) [matrix of math nodes, nodes in empty cells, %nodes=draw ] { & & & & & & 1 & 1 & 1 & 1 & 1 & 1 & & \\ & 0 & 0 & 0 & 1 & 0 & 0 & 1 & 1 & 1 & 1 & 0 & 1 &[10mm] \addend \\ + & 0 & 0 & 0 & 0 & 1 & 0 & 1 & 1 & 0 & 1 & 1 & 1 & \augend \\ & 0 & 0 & 0 & 1 & 1 & 1 & 1 & 1 & 0 & 1 & 0 & 0 & \sumOut \\ }; \draw[-,color=black,semithick] (m-3-2.south west) -- (m-3-13.south east); \end{tikzpicture} \label{binary_integer_addition}$$