# Lesson 3.7. Shifting operators

In C, there are two operators for shifting bits:

• << is left shift
• >> is right shift
s = a >> b;

We can see in the example above that these operators accept two operands, so they are binary operators. The first operand (a) is the variable to shift. The second operand is the number of bits of the shift. If b is 2, the variable a will be shifted by two bits to the right.

## Example 1

Consider the following example:

unsigned char a= 0x3C;
printf ("a << 1 = %d\n", a<<1 );

To simulate the result of this operator, it is necessary to follow the same method as for bitwise operators. Let's start by converting the variable a into binary :

$$(3C)_{16} = (0011\:1100)_2$$

Now, let's shift the binary word to the left by 1 bit:

$$(0011\:1100)_2 << 1 = (0111\:1000)_2$$

Note that the shift is not a circular permutation, the left bit (high order) does not return to the right bit. Here, the left bit is lost. With a left shift, the low-order bit (right-hand bit) is always replaced by a zero.

It is now enough to convert this result in base 10 to obtain the display of the program:

$$(0111\:1000)_2 = (120)_{10}$$

Let's check this result:

## Example 2

Let's consider the following example:

unsigned char a= 0x3C;
printf ("a >> 3 = %d\n", a>>3 );

The conversion of the variable a to binary is identical to the previous example.

$$(3C)_{16} = (0011\:1100)_2$$

The general scheme of the shift is represented by:

$$(0011\:1100)_2 >> 3 = (0000\:0111)_2 = (7)_{10}$$

Let's check this result:

## Shifting and multiplying

A binary shift is the same as a multiplication or division. When we shift to the left, we multiply by two:

• a << 1 is equivalent to $$2 \times a$$
• a << 2 is equivalent to $$4 \times a$$
• a << 3 is equivalent to $$8 \times a$$
• a << 4 is equivalent to $$16 \times a$$
• a << n is equivalent to $$2^n \times a$$

For right-hand shifts, if we omit the decimal part of the result, we can also consider that :

• a >> n is equivalent to $$\dfrac{a}{2^n}$$

To guarantee equivalence between these multiplications and the shifts, the bit that is entered in the binary word is always a zero, unless the variable is negative.

As shifts are generally only used on unsigned, remember that in this case, zeros are always inserted.

## Exercise

Complete the following program to perform two operations:

• A 3-bit shift to the left of the variable a ;
• A multiplication by 8 of the variable a.

The results will be assigned to the variables shift and mult respectively.

#include <stdio.h>

int main(void) {
unsigned char a=0x12;
unsigned char mult, shift;

// shift = a shifted 3 bits to the left
// COMPLETE HERE

// mult = a multiplied by 8
// COMPLETE HERE

// Display the results
printf ("a << 3 = %d\n", shift);
printf ("a * 8 = %d\n", mult);

return 0;
}

## Quiz

What does the following program display?

unsigned char a=3;
printf ("%d", a << 1 );

What does the following program display?

unsigned char a=3;
printf ("%d", a >> 1 );

What does the following program display?

unsigned char a=0x0F;
printf ("%d", a << 4 );

What does the following program display?

unsigned char a=0x0F;
printf ("%d", a >> 4 );

Without converting to binary, find what the following program displays.

unsigned char a=1;
printf ("%d", a << 5 );

Without converting to binary, find what the following program displays.

unsigned char a=26;
printf ("%d", a >> 2 );

Without converting to binary, find what the following program displays.

unsigned int a=0xA4D912AB;
printf ("%X", a << 4 );