Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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)?

share|improve this question
2  
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
add comment

2 Answers

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.

References

\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

enter image description here

share|improve this answer
    
@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
add comment

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}

\newcommand{\addend}{\text{\textsl{\color{gray}{Addend}}}}
\newcommand{\augend}{\text{\textsl{\color{gray}{Augend}}}}
\newcommand{\sumOut}{\text{\textsl{\color{gray}{Sum}}}}

\begin{document}
Calculate the binary sum $0001\;0011\;1101+0000\;1011\;0111$.
\begin{equation}
\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}
\end{equation}
\end{document}

Which outputs:

Binary addition showing carries Because each digit is a node it is possible to enhance the presentation of the sum by drawing colored arrows between the nodes (which can be useful in explaining borrowing in in binary subtraction for example).

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.