In order to examine bit by bit the number, the scheme might be like this
\def\temp{10111}
\newcount\bitnumber
\def\slowbinary#1{%
\ifx\relax#1% end
\else
\advance\bitnumber by 1
\ifcase#1\relax\docasezero\or\docaseone\fi
\expandafter\slowbinary
\fi}
\def\processbinary#1{\bitnumber=-1 % reset the counter (we will start at 0)
\expandafter\slowbinary#1\relax}
\def\docasezero{\message{^^JZero seen}}
\def\docaseone{\message{^^JOne seen}}
\processbinary\temp
\processbinary{1010}
In case you need to start from the less significant bit, you have to reverse the number first.
\makeatletter
\def\processbinaryrev#1{%
\bitnumber=-1 % reset the counter (we will start at 0)
\@reverse{#1}%
\expandafter\slowbinary\@esrever \relax}
\def\@reverse#1{%
\edef\@temp{#1}%
\def\@esrever{}%
\loop\unless\ifx\@temp\@empty
\edef\@esrever{\expandafter\@car\@temp\@nil\@esrever}%
\edef\@temp{\expandafter\@cdr\@temp\@nil}%
\repeat}
\makeatother
\processbinaryrev\temp
There are many loops around for reversing strings. Maybe also PGF has some.
Instead of \temp
you can use \pgfmathresult
or the first argument to \pgfmathdectobase
.
Of course you'll give more meaningful actions to \docasezero
and \docaseone
; these actions can use the value of \bitnumber
. You can define a set of macros as
\def\docasezero{\csname casezero\romannumeral\bitnumber\endcsname}
\def\docaseone{\csname caseone\romannumeral\bitnumber\endcsname}
\def\casezero{What to do when bit 0 is 0}
\def\caseone{What to do when bit 0 is 1}
\def\casezeroi{What to do when bit 1 is 0}
\def\caseonei{What to do when bit 1 is 1}
\def\casezeroii{What to do when bit 2 is 0}
\def\caseoneii{What to do when bit 2 is 1}
...
Here is a complete example (where I've changed a bit the programming style, using @
and constants provided by the LaTeX kernel):
\documentclass[a4paper]{article}
\makeatletter
\newcount\@bitnumber
\def\@slowbinary#1{%
\ifx\relax#1% end
\else
\advance\@bitnumber\@ne
\ifcase#1\relax\do@casezero\or\do@caseone\fi
\expandafter\@slowbinary
\fi}
\def\@reverse#1{%
\edef\@temp{#1}%
\def\@esrever{}%
\loop\unless\ifx\@temp\@empty
\edef\@esrever{\expandafter\@car\@temp\@nil\@esrever}%
\edef\@temp{\expandafter\@cdr\@temp\@nil}%
\repeat}
% \def\processbinary#1{\@bitnumber\m@ne
% \expandafter\@slowbinary#1\relax}
\def\processbinaryrev#1{\@reverse{#1}\@bitnumber=-1
\expandafter\@slowbinary\@esrever \relax}
\def\do@casezero{\csname casezero\romannumeral\@bitnumber\endcsname\relax}
\def\do@caseone{\csname caseone\romannumeral\@bitnumber\endcsname\relax}
\makeatother
\def\casezero{What to do when bit 0 is 0\par}
\def\caseone{What to do when bit 0 is 1\par}
\def\casezeroi{What to do when bit 1 is 0\par}
\def\caseonei{What to do when bit 1 is 1\par}
\def\casezeroii{What to do when bit 2 is 0\par}
\def\caseoneii{What to do when bit 2 is 1\par}
\def\casezeroiii{What to do when bit 3 is 0\par}
\def\caseoneiii{What to do when bit 3 is 1\par}
\def\casezeroiv{What to do when bit 4 is 0\par}
\def\caseoneiv{What to do when bit 4 is 1\par}
\def\casezerov{What to do when bit 5 is 0\par}
\def\caseonev{What to do when bit 5 is 1\par}
\def\temp{10111}
\begin{document}
\temp
\processbinaryrev\temp
\bigskip
101110
\processbinaryrev{101110}
\end{document}

{xstring}
package for traversing strings, so think that you should be able to traverse it with\StrLeft
along with\StrGobbleLeft
.