# Bitwise operator in pseudo-code

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.

-
Can you please explain what you mean by "I used power of 2"? – Hendrik Vogt Mar 25 '11 at 11:04
@Hendrik: Apparently x \cdot 2^n and x / 2^n, where n is the shift amount. – Andrey Vihrov Mar 25 '11 at 11:08
@Andrey: Thanks, now I get it - in pseudo-code that's not too bad. – Hendrik Vogt Mar 25 '11 at 11:26
@HendrikVogt: Notice that << behaves differently than power when running out of bits. – user877329 Feb 16 '15 at 12:18

## 5 Answers

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.
-
Ok I will use the Call (I didn't know it). – BenjaminB Mar 25 '11 at 11:45

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{\mathrel{\&}}
\newcommand*\BitOr{\mathrel{|}}
\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 recomment 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.

-
I agree that your point is valid. A yet better approach would be to use the C-style notation and add a list of operators and their meanings somewhere in the beginning of the document. This eliminates any possible confusion. – Andrey Vihrov Jul 23 '11 at 9:32
How about \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) ) – Nick T Nov 24 '15 at 15:43
@NickT See tex.stackexchange.com/q/77646/42. – Konrad Rudolph Nov 24 '15 at 16:24

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.

-
\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, 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

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.

-