21

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.

Martin Scharrer
  • 262,582
BenjaminB
  • 313
  • 1
  • 2
  • 7

5 Answers5

27

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.

Konrad Rudolph
  • 39,394
  • 22
  • 107
  • 160
13

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:

  • continue using algorithmicx and select a human-readable name for the operation: \State $x \gets \Call{ShiftLeft}{x, 3}$;
  • use the listings package and typeset the actual C++ program with comments.
Andrey Vihrov
  • 22,325
12

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}

enter image description here

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.

Leo Liu
  • 77,365
  • \ll and \gg means more "very lower" and "very greater" rather than bit level shift. – BenjaminB Mar 25 '11 at 11:06
  • @Ubiquité: Yes, I know that. But they are actully used in some algorithm descriptions, especially in figures. For example, http://en.wikipedia.org/wiki/SHA-1 – Leo Liu Mar 25 '11 at 11:17
  • Plus, in the 'normal' representation of the programming construct, they are not, I think, kerned. – Brent.Longborough Mar 25 '11 at 13:34
  • @Brent But they look better when kerned. Pretty-printed pseudo-code can take some freedom with operators. Many authors do this and in some cases (Haskell and ML come to mind) this is actually encouraged and something of a standard. – Konrad Rudolph Mar 25 '11 at 15:10
  • @Konrad: Knuth's WEB comes to mind too. weave tex.web and then run pdftex tex. – TH. Mar 25 '11 at 20:31
4

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.

4

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.

Werner
  • 603,163
dakota
  • 141
  • 3