\myheading{Remainder of division}

\leveldown{}

\myheading{Remainder of division by modulo $2^{n}$}

... can be easily computed with AND operation.
If you need a random number in range of 0..16, here you go: rand()\&0xF.
That helps sometimes.

For example, you need a some kind of wrapping counter variable which always should be in 0..16 range. What you do?
Programmers often write this:

\begin{lstlisting}[style=customc]
int counter=0;
...
counter++;
if (counter==16)
    counter=0;
\end{lstlisting}

But here is a version without conditional branching:

\begin{lstlisting}[style=customc]
int counter=0;
...
counter++;
counter=counter&0xF;
\end{lstlisting}

As an example, this I found in the git source code:

\begin{lstlisting}[style=customc]
char *sha1_to_hex(const unsigned char *sha1)
{
        static int bufno;
        static char hexbuffer[4][GIT_SHA1_HEXSZ + 1];
        static const char hex[] = "0123456789abcdef";
        char *buffer = hexbuffer[3 & ++bufno], *buf = buffer;
        int i;

        for (i = 0; i < GIT_SHA1_RAWSZ; i++) {
                unsigned int val = *sha1++;
                *buf++ = hex[val >> 4];
                *buf++ = hex[val & 0xf];
        }
        *buf = '\0';

        return buffer;
}
\end{lstlisting}

( \url{https://github.com/git/git/blob/aa1c6fdf478c023180e5ca5f1658b00a72592dc6/hex.c} )

This function returns a pointer to the string containing hexadecimal representation of SHA1 digest \\
(like "4e1243bd22c66e76c2ba9eddc1f91394e57f9f83").
But this is plain C and you can calculate SHA1 for some block, get pointer to the string, then calculate SHA1 for another block, get pointer to the string,
and both pointers are still points to the same string buffer containing the result of the second calculation.
As a solution, it's possible to allocate/deallocate string buffer each time, but more hackish way is to have several buffers (4 are here) and fill the next each time.
The \textit{bufno} variable here is a buffer counter in 0..3 range.
Its value increments each time, and its value is also always
kept in limits
by AND operation (\textit{3 \& ++bufno}).

The author of this piece of code (seemingly Linus Torvalds himself) went even further and forgot (?)
to initialize \textit{bufno} counter variable, which will
have random garbage at the function start.
Indeed: no matter, which buffer we are starting each time!
This can be mistake which isn't affect correctness of the code, or maybe this is left so intentionally -- I don't know.

\myhrule{}

Perhaps, many of us had used a code like that:

\begin{lstlisting}[style=customc]
cnt=0;

for (...)
{
    do_something();
    cnt=cnt+1;
    if ((cnt % 1000)==0);
        print ("heartbeat: %d processed", cnt);
\end{lstlisting}

This is how you can use remainder from division for occasional logging: printing will happen once in 1000 steps.

However, another way is:

\begin{lstlisting}[style=customc]
cnt=0

for (...)
{
    do_something();
    cnt=cnt+1;
    if ((cnt&0xfff)==0);
        print ("heartbeat: %d processed", cnt);
\end{lstlisting}

Now printing will occur once in 0x1000 or 4096 steps. This is also getting remainder from division.

\levelup{}

