16

Is there a package to perform binary arithmetic like addition, subtraction, and so on? Ex: 1101 + 0001 = 1110.

BUT not as text, like I've typed in here!

Also, is there any other packager rather than xlop for decimal arithmetic operations?

Peter Grill
  • 223,288

4 Answers4

20

The math functions built into pgf can be used:

enter image description here

Notes:

  • An integer with prefix 0b or 0B is interpreted as a binary number and is automatically converted to base 10. Hence the prefixes in 0b#1 + 0b#2. It should be noted that this feature of treating numbers with a leading 0 as an octal number can not be disabled as per Martin Scharrer's answer at Unexpected results from pgfmath functions with numbers with leading 0.

  • \pgfmathbin{x} converts an integer to a binary representation.

  • \pgfmathprintnumber is optional and is used to format the number. In this case it automatically detects that the decimal value of the addition is an integer and produces 102 instead of 102.0 as is the case for the first decimal addition.

Code:

\documentclass{article}
\usepackage{pgf}
\newcommand{\BinaryAdd}[2]{%
    {#1}_2 + {#2}_2 = \pgfmathbin{0b#1 + 0b#2}% compute binary addition
    \pgfmathresult_2% format output to make it clear it is base 2
}%
\newcommand{\BinarySub}[2]{%
    {#1}_2 - {#2}_2 = \pgfmathbin{0b#1 - 0b#2}% compute binary subtraction
    \pgfmathresult_2% format output to make it clear it is base 2
}%

\newcommand{\DecimalAdd}[2]{% #1 + #2 = \pgfmathparse{#1 + #2}% compute addition \pgfmathprintnumber{\pgfmathresult}% format output }% \newcommand{\DecimalSub}[2]{% #1 - #2 = \pgfmathparse{#1 - #2}% compute subtraction \pgfmathprintnumber{\pgfmathresult}% format output }% \begin{document} $\BinaryAdd{101}{0001}$\par $\BinarySub{101}{0001}$

\bigskip $\DecimalAdd{101}{0001}$\par $\DecimalSub{101}{0001}$ \end{document}

Peter Grill
  • 223,288
10

The fmtcount package allows for a variety of counter formatting options, including printing decimal values as binary (with some leading zeros, if needed).

The following minimal example provides \binnum[<width>]{<num>} the prints <num> in binary of width <width> (default is 4), and allows for calculations. For proper spacing, it should be used in math mode:

enter image description here

\documentclass{article}
\setlength{\textwidth}{400pt}% Just for this example
\usepackage{fmtcount}% http://ctan.org/pkg/fmtcount
\newcounter{mycounter}%
\newcommand{\binnum}[2][4]{%
  \setcounter{mycounter}{\numexpr #2\relax}%
  \padzeroes[#1]{\binary{mycounter}}%
}
\begin{document}
$13+1=\binnum{13}_2+\binnum{1}_2=\binnum{13+1}_2$ \par
$1+2+4+8+16+32=\binnum{1}_2+\binnum{2}_2+\binnum{4}_2+\binnum{8}_2+\binnum{16}_2+\binnum{32}_2=\binnum[8]{1+2+4+8+16+32}_2$
\end{document}​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​

If you're interested, you can use this updated version that defines a starred version of \binnum[*][<width>]{<num>}. The defaults are the same with the unstarred version printing the base (2) as well. The starred version does not print the base:

\usepackage{xparse}% http://ctan.org/pkg/xparse
\NewDocumentCommand{\binnum}{s O{4} m}{%
  \setcounter{mycounter}{\numexpr #3\relax}%
  \padzeroes[#2]{\binary{mycounter}}\IfBooleanTF{#1}{}{_2}%
}

xparse provides the command definition interface.

Werner
  • 603,163
6

As usual, I recommend TikZ:

\documentclass[parskip]{scrartcl}
\usepackage[margin=15mm]{geometry}
\usepackage{tikz}

\begin{document}

\newcommand{\ci}[1]{\pgfmathparse{#1}\pgfmathresult}

\pgfmathsetmacro{\numa}{33.5}
\pgfmathsetmacro{\numb}{15.4}
\begin{tabular}{|l|r|r|r|r|} \hline
 & add & sub & mult & div \\
decimal & \ci{\numa+\numb} & \ci{\numa-\numb} & \ci{\numa*\numb} & \ci{\numa/\numb} \\ \hline
binary & \ci{bin(\numa+\numb)} & \ci{bin(\numa-\numb)} & \ci{bin(\numa*\numb)} & \ci{bin(\numa/\numb)} \\ \hline
\end{tabular}
\end{document}

enter image description here

Tom Bombadil
  • 40,123
2

The following implements some of the functions for using radix 2 numbers as input:

\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
\cs_new:cpn {@binary} #1 { \int_to_binary:n { #1 } } % necessary for LaTeX2e infrastructure

\cs_new:Npn \binary_sum:nn #1 #2
  {
   \int_eval:n { \int_from_binary:n { #1 } + \int_from_binary:n { #2 } }
  }

\NewDocumentCommand{\evalbinsum}{ m m }
  {
   \int_to_binary:n { \binary_sum:nn {#1} {#2} }
  }

\NewDocumentCommand{\binsetcounter}{ m m }
  {
   \setcounter{#1}{\int_from_binary:n {#2}}
  }

\ExplSyntaxOff

\NewDocumentCommand{\showbinsum}{ m m }
  {
   #1_2 + #2_2 = \evalbinsum{#1}{#2}_2
  }

\makeatletter
\newcommand{\binary}[1]{\expandafter\@binary\csname c@#1\endcsname}
\makeatother

\begin{document}

$\showbinsum{1101}{0001}$ % show a sum of radix 2 numbers

\newcounter{mycnt}

\binsetcounter{mycnt}{11001} % set a counter using a radix 2 number

$\arabic{mycnt}=\binary{mycnt}_2$ % verification

\end{document}

With \binary{mycnt} one gets the radix 2 representation of the counter's value (similar to \arabic).

egreg
  • 1,121,712