\documentclass[]{article}
\usepackage{amsmath,graphicx,supertabular,hyperref,tabularx,ifthen}
\title{TIP \#343: A Binary Specifier for [format/scan]}
\date{December 03, 2008}
\author{Alexandre Ferrieux}
\urlstyle{sf}
\setlength{\parskip}{1ex}
\setlength{\parindent}{0pt}
\def\tipversion$#1${\texttt{\$#1\$}}
\def\tiplangle#1{\ensuremath{<}}
\def\tiprangle#1{\ensuremath{>}}
\def\tipbar#1{\ensuremath{|}}
\def\tipmail#1#2{\(\langle\){\small\expandafter\url{#1@#2}}\(\rangle\)}
\ifx\pdfoutput\undefined
\newcommand{\tipimage}[2]{\typeout{Make sure you download #1.eps}\ifthenelse{\lengthtest{0.8\textwidth>#2}\and\lengthtest{0pt<#2}}{\includegraphics{#1.eps}}{\includegraphics[width=0.8\textwidth]{#1.eps}}}
\newcommand{\tipxref}[1]{}
\newcommand{\tipxrefend}{}
\else
\newcommand{\tipimage}[2]{\typeout{Make sure you create #1.pdf}\ifthenelse{\lengthtest{0.8\textwidth>#2}\and\lengthtest{0pt<#2}}{\includegraphics{#1.pdf}}{\includegraphics[width=0.8\textwidth]{#1.pdf}}}
\pdfcatalog{/PageMode /UseOutlines}
\newcommand{\tipxref}[1]{\pdfannotlink attr {/C [0.5 0.5 1.0] /Border [0 0 1]} goto name {#1}}
\newcommand{\tipxrefend}{\pdfendlink}
\fi
\newenvironment{tipabstract}{\begin{abstract}}{\end{abstract}}
\begin{document}\maketitle
\begin{center}\begin{tabularx}{\linewidth}{|r@{: }X|}\hline
\textbf{TIP \#343}&\textbf{A Binary Specifier for [format/scan]}\\\hline
Author&

Alexandre Ferrieux \tipmail{alexandre.ferrieux}{gmail.com}
\\
Created&Wednesday, \(\text{3}^{\text{rd}}\) December 2008\\
Type&Project\\
Tcl Version&\(8.6\)\\
State&Final\\
Vote&Done\\
Version&\tipversion$Revision: 1.3 $\\
Keywords&Tcl, binary\\
Post History&\\
\hline\end{tabularx}\end{center}
\thispagestyle{empty}\pagestyle{empty}
\begin{tipabstract}

This TIP proposes to add a \%b specifier to the \textbf{format} and \textbf{scan} commands for working with integers in base-2 representation.
\end{tipabstract}
\tableofcontents\setcounter{page}{0}\clearpage\pagestyle{plain}

\section{Background}

The \textbf{format} and \textbf{scan} commands already have decimal, hexadecimal, and octal support, but people wanting binary today resort to a hack, namely going hexadecimal first and then \textbf{string map}ping the hex digits to their four-bits binary representations. We also already have the ``0b'' notation as input for \textbf{expr}. This lack is inelegant.

\section{Proposed Change}

This TIP proposes to reuse the existing binary representation machinery to expose a new \textbf{\%b} specifier:
\begingroup\small\begin{verbatim}
	format %b 5
	=> 101
	scan 000101 %b x;set x
	=> 5
\end{verbatim}
\endgroup

The good properties of the existing code make the addition really painless, in that it automagically combines with the size (l,ll), width (digits), and prefix (\#) modifiers:
\begingroup\small\begin{verbatim}
	format %#06b 5
	=> 0b0101
	format %llb [expr {(2**72-1)/7}]
	1001001001001001001001001001001001001001001001001001001001001001001001
\end{verbatim}
\endgroup

\section{Rationale}

That is really low-hanging fruit. All the pieces are in place, it's just a matter of exposition. The binary representation is a nice pedagogic tool, and having it handy (rather than with a hack) is a bonus.

\section{Reference Implementation}

See Patch 2368084 [\url{https://sourceforge.net/support/tracker.php?aid=2368084}].

\section{Copyright}

This document has been placed in the public domain.

\section{Colophon}

\textit{TIP AutoGenerator --- written by Donal K. Fellows}
\end{document}
