# Typesetting binary arithmetic

I'm stuck trying to typeset the following in math mode. I cannot get the spacing correct.

       0100   [4 in decimal]
x 1100   [12 in decimal]
------
+    0000   [0100 x 0]
+   0000    [0100x 0, shifted left]
+  0100     [0100x 1, shifted left two positions]
+ 0100      [0100x 1, shifted left three positions]
=========
0110000   [48 decimal]


Can this be achieved in math mode, or is is better / easier to just stick with pre-formatted text?

• What did you try before? Looks like either an array or a few \hphantom{0} should do. Oct 8 '13 at 13:50
• \hphantom did it. Still a novice with LaTeX, thanks for introducing me to a useful element. I'd guessed there must be something like that, but didn't find it. Oct 8 '13 at 14:00

\hphantom has already been suggested, but making this look really nice was a fun diversion.

\documentclass{article}
\usepackage{amsmath}

\newcommand{\0}{\phantom{0}}
\begin{document}

\begin{alignat*}{4}
&& 0100 & \quad && \text{[\,4 in decimal]}\\
&& \underline{\strut{}\times 1100} &&& \text{[\,12 in decimal]}\\
& + & 0000 &&& \text{[\,$0100\times 0$]}\\
& + & 0000\0 &&& \text{[\,$0100\times 0$, shifted left]}\\
& + & 0100\0\0 &&& \text{[\,$0100\times 1$, shifted left two positions]}\\
& \underline{\strut+} & \underline{\strut\0 0100\0\0\0} &&&
\text{[\,$0100\times 1$, shifted left three positions]}\\
&& \0 0110000 &&& \text{[\,48 decimal]}
\end{alignat*}

\end{document} • @DavidCarlisle -- yes, stolen, i do believe. your approach, while solid, has a couple of flaws -- you should have used \times in the explications, and the widths of the rules don't encompass the signs of operation as posited in the original question. (details, sir, details!) Oct 8 '13 at 15:35
• Better yet: \begingroup\catcode?=\active\def?{\hphantom{0}}\begin{alignat*}{2} ;-) Oct 8 '13 at 15:36
• @egreg -- oh, golly, yes, but for once, i was trying to do something entirely within the latex canon, and not drop back to plain tex, as is my wont. Oct 8 '13 at 15:40 \documentclass{article}
\usepackage{array}
\setlength\extrarowheight{2pt}
\begin{document}
\begin{array}{@{}*{9}{c@{}}>{\quad[}l<{]}@{}} & && & &0&1&0&0& 4 in decimal\\ & &&\times & &1&1&0&0& 12 in decimal\\ \cline{5-9} +& & & & &0&0&0&0& 0100 x 0\\ +& & & &0&0&0&0& & 0100x 0, shifted left\\ +& & &0&1&0&0& & & 0100x 1, shifted left two positions\\ +&\phantom{0} &0&1&0&0& & &&0100x 1, shifted left three positions\\ \cline{3-9} \noalign{\kern2pt} \cline{3-9} & &0&1&1&0&0&0&0& 48 decimal \end{array}
\end{document}


Just for fun! ;-)

The table is typeset by

\binmult{4}{12}


Here's the code.

\documentclass{article}
\usepackage{xparse,booktabs}

\ExplSyntaxOn
\NewDocumentCommand{\binmult}{mm}
{
\david_binmult:nn { #1 } { #2 }
}

\int_new:N \l_david_binmult_first_int
\int_new:N \l_david_binmult_second_int
\int_new:N \l_david_binmult_bits_int
\int_new:N \l__david_binmult_cycle_int

\tl_new:N \l_david_binmult_first_tl
\tl_new:N \l_david_binmult_second_tl
\tl_new:N \l_david_binmult_second_rev_tl
\tl_new:N \l_david_binmult_product_tl
\tl_new:N \l_david_binmult_tablebody_tl

\seq_new:N \l_david_binmult_partial_products_seq
\seq_new:N \l_david_binmult_partial_descr_seq

{% #1 should be a tl, #2 an integer denomination
\prg_replicate:nn { \int_max:nn { #2 - \tl_count:N #1 } { 0 } }
{
\tl_put_left:Nn #1 { 0 }
}
}

\cs_new_protected:Nn \david_binmult:nn
{
% store the data
\int_set:Nn \l_david_binmult_first_int { #1 }
\int_set:Nn \l_david_binmult_second_int { #2 }
\tl_set:Nx \l_david_binmult_first_tl { \int_to_bin:n { #1 } }
\tl_set:Nx \l_david_binmult_second_tl { \int_to_bin:n { #2 } }
\tl_set:Nx \l_david_binmult_product_tl { \int_to_bin:n { #1 * #2 } }
\int_set:Nn \l_david_binmult_bits_int
{
\int_max:nn { \tl_count:N \l_david_binmult_first_tl }
{ \tl_count:N \l_david_binmult_second_tl }
}
{
2*\l_david_binmult_bits_int-1
}
\tl_set_eq:NN \l_david_binmult_second_rev_tl \l_david_binmult_second_tl
\tl_reverse:N \l_david_binmult_second_rev_tl
% compute the partial products
\seq_clear:N \l_david_binmult_partial_products_seq
\seq_clear:N \l_david_binmult_partial_descr_seq
\int_zero:N \l__david_binmult_cycle_int
\tl_map_inline:Nn \l_david_binmult_second_rev_tl
{
\int_compare:nTF { ##1 = 0 }
{
\seq_put_right:Nx \l_david_binmult_partial_products_seq
{
\prg_replicate:nn { \l_david_binmult_bits_int } { 0 }
\prg_replicate:nn { \l__david_binmult_cycle_int } { \exp_not:N \hphantom {0} }
}
}
{
\seq_put_right:Nx \l_david_binmult_partial_products_seq
{
\tl_use:N \l_david_binmult_first_tl
\prg_replicate:nn { \l__david_binmult_cycle_int } { \exp_not:N \hphantom {0} }
}
}
\seq_put_right:Nx \l_david_binmult_partial_descr_seq
{
[
$\tl_use:N \l_david_binmult_first_tl \times ##1$
\int_case:nnF { \l__david_binmult_cycle_int }
{
{0}{}
{1}{,~shifted~left}
}
{,~shifted~left~\david_binmult_number:n { \l__david_binmult_cycle_int }~positions}
]
}
\int_incr:N \l__david_binmult_cycle_int
}
% build the tabular
\tl_clear:N \l_david_binmult_tablebody_tl
\tl_put_right:Nn \l_david_binmult_tablebody_tl
{
& $\tl_use:N \l_david_binmult_first_tl$ & [$#1$~in~decimal] \\
& ${\times}\;\tl_use:N \l_david_binmult_second_tl$ & [$#2$~in~decimal] \\
\cmidrule(r){2-2}
}
\int_step_inline:nnnn { 1 } { 1 } { \seq_count:N \l_david_binmult_partial_products_seq }
{
\tl_put_right:Nx \l_david_binmult_tablebody_tl
{
$+$ &
\seq_item:Nn \l_david_binmult_partial_products_seq { ##1 } &
\seq_item:Nn \l_david_binmult_partial_descr_seq { ##1 } \exp_not:N \\
}
}
\tl_put_right:Nn \l_david_binmult_tablebody_tl
{
\cmidrule(r){1-2}
& $\tl_use:N \l_david_binmult_product_tl$ & [$\int_to_arabic:n { #1 * #2 }$~in~decimal]
}
% print the table
\begin{tabular}{@{} c r l @{}}
\tl_use:N \l_david_binmult_tablebody_tl
\end{tabular}
}

\cs_new:Nn \david_binmult_number:n
{
\int_case:nn { #1 }
{
{0}{zero}
{1}{one}
{2}{two}
{3}{three}
{4}{four}
{5}{five}
{6}{six}
{7}{seven}
{8}{eight}
}
}
\ExplSyntaxOff

\begin{document}

\binmult{4}{12}

\bigskip

\binmult{12}{12}

\bigskip

\binmult{13}{35}

\end{document}
` 