I am translating a C++ program into pseudo-code using the algorithmicx
package.
Do you know how I can represent the C bitwise operator like the shifts (<< and >>) ?
Until now I used power of 2 but it's not very convenient.
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It only takes a minute to sign up.
Sign up to join this communityI am translating a C++ program into pseudo-code using the algorithmicx
package.
Do you know how I can represent the C bitwise operator like the shifts (<< and >>) ?
Until now I used power of 2 but it's not very convenient.
Pseudocode has a different purpose compared to the actual programs. It should convey ideas, not implementation, and as such should be as close to the natural language as possible. Therefore I think it's not good to introduce programming language-specific syntax in the algorithm listing.
I suggest one of these options:
algorithmicx
and select a human-readable name for the operation: \State $x \gets \Call{ShiftLeft}{x, 3}$
;listings
package and typeset the actual C++ program with comments.I advise against Andrey’s solution. He is right that in general pseudocode should be independent of a specific machine or language.
But this breaks down with bit operations. Bit operations do suggest a specific underlying architecture, and the bit operators follow an established nomenclature.
You don’t make the code more readable by ignoring this convention – in fact, you do the opposite.
I have the following commands defined in my thesis template:
\newcommand*\BitAnd{\mathbin{\&}}
\newcommand*\BitOr{\mathbin{|}}
\newcommand*\ShiftLeft{\ll}
\newcommand*\ShiftRight{\gg}
\newcommand*\BitNeg{\ensuremath{\mathord{\sim}}}
(The command names follow the naming convention of the algorithmicx
package which I can recommend for typesetting algorithms.)
That said, you should second-guess your reason for using bit operations in the first place – often they are only used to achieve specific optimisations, in which case they have no place in a pseudo-code. On the other hand, sometimes (and it sounds as if this may be the case for you) they have a legitimate purpose.
\BitXor
? I tried \newcommand{\BitXor}{\mathrel{\^}}
and it's giving me problems (in MathJax, maybe something strange there, but the other commands worked (non-starred) )
For symbols, you can use \ll
and \gg
for shifting, and \lll
, \ggg
for rotating.
\documentclass{article}
\usepackage{amssymb}
\begin{document}
$a\land b$, $a\lor b$, $\lnot a$, $a\oplus b$
$a\ll b$, $a\gg b$, $a\lll b$, $a\ggg b$
\end{document}
You can also define some functions:
\usepackage{amsmath}
\DeclareMathOperator\shl{shl}% shift left
\DeclareMathOperator\shr{shr}% shift right
\DeclareMathOperator\rol{rol}% rotate left
\DeclareMathOperator\ror{ror}% rotate right
and use $\shl(a,n)$
etc. in the algorithm.
weave tex.web
and then run pdftex tex
.
Use the \verb
command, e. g.
\verb|<<|
and
\verb|>>|
if you need to have those operators displayed exactly as they would be typed in a program, and do not want to use other packages for that.
In mathematics \ll
is often used for "much less than" and using both in one document creates a need to distinguish this symbol from the bit shift operator.
My suggestion is to combine the answers of Konrad and PointedEars thusly:
\DeclareMathOperator\ShiftLeft{\texttt{<<}}% shift left
The \verb command is overpowered for this purpose and has some restrictions which make it harder to work with. I use \texttt{<<}
instead, as it may more easily be included into macros. It's also a good idea to make a macro out of whatever you settle on, so in the future you only have to change it in one place if you change your mind.
x \cdot 2^n
andx / 2^n
, wheren
is the shift amount.