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.

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:

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:

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.

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;
}
```

**What does the following program display?**

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

Check
Bravo! $$ (0000\:0011)_2 << 1 = (0000\:0110)_2 = (6)_{10} $$
Try again...

**What does the following program display?**

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

Check
Bravo! $$ (0000\:0011)_2 >> 1 = (0000\:0001)_2 = (1)_{10} $$
Try again...

**What does the following program display?**

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

Check
Bravo! $$ (0000\:1111)_2 << 1 = (1111\:0000)_2 = (240)_{10} $$
Try again...

**What does the following program display?**

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

Check
Bravo! $$ (0000\:1111)_2 >> 1 = (0000\:0000)_2 = (0)_{10} $$
Try again...

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

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

Check
Bravo! The shift is also a multiplication. The result is therefore \( a \times 2^{5} = a \times 32 \)
Try again...

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

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

Check
Bravo! The shift is also a division. The result is therefore \( \dfrac {a} {2^{2}} = 6 \) because we keep only the integer part.
Try again...

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

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

Check
Bravo ! In hexadecimal, each symbol is coded on 4 bits. As we shift 4 bits, we shift one symbol.
Try again...

- Learn C programming
- Lesson 1.1. History of C programming language
- Lesson 1.2. First program
- Lesson 1.3. Compilation
- Lesson 1.4. Preprocessor directives
- Lesson 1.5. Which compiler to choose?
- Lesson 1.6. Flowchart
- Lesson 2.1. Types and variables
- Lesson 2.2. Integers
- Lesson 2.3. Floating point numbers
- Lesson 2.4. Characters
- Lesson 2.5. Variables initialization
- Lesson 2.6. Ariane flight 501
- Lesson 3.1. Arithmetic operators
- Lesson 3.2. modulo
- Lesson 3.3. Operators and types
- Lesson 3.4. Type casting
- Lesson 3.5. Bitwise operators
- Lesson 3.6. Bitwise operators details
- Lesson 3.8. Assignment operators
- Lesson 3.9. Increment and decrement operators
- Lesson 3.10. Comparison operators
- Lesson 3.11. Logical operators
- Lesson 3.12. Operator precedence
- Lesson 4.1. printf
- Lesson 4.2. scanf
- Lesson 4.3. putchar
- Lesson 5.1. Conditional branching (if...else)
- Lesson 5.2. Nested if and indentation
- Lesson 5.3. Checking intervals
- Lesson 5.4. Ternary Operator ?:
- Lesson 5.5. switch..case statement
- Lesson 5.6. Break keyword in switch..case
- Lesson 6.1. do..while loop in C
- Lesson 6.2. While loop in C
- Lesson 6.3. For loop in C
- Lesson 6.4. How to choose the right loop in C ?
- Lesson 6.5. Exercices on loops in C
- Lesson 7.1. Bit masking
- Lesson 7.2. Bit masking, how to clear a bit in C?
- Lesson 7.3. Bit masking, how to set a bit in C?
- Lesson 7.4. Bit masking, how to toggle a bit in C?
- Lesson 7.5. Bit masking, how to check a single bit in C?
- Lesson 7.6. Overview of bit masking
- Lesson 8.1. Basic syntax of C functions
- Lesson 8.2. Calling C functions
- Lesson 8.3. Void keyword
- Lesson 8.4. Return keyword
- Lesson 8.5. Variable scope
- Lesson 8.6. Global variables
- Lesson 8.7. Static variables
- Lesson 8.8. Random numbers in C
- Lesson 8.9. Mathematical functions in C
- Lesson 9.1. Syntax of C arrays
- Lesson 9.2. Array initialization
- Lesson 9.3. Multidimensional arrays
- Lesson 9.4. How C arrays are stored in memory?
- Lesson 9.5. Arrays and functions
- Lesson 9.6. Exercises on C arrays
- Lesson 10.1. Strings in C
- Lesson 10.2. Null terminated string
- Lesson 10.3. The string.h library
- Lesson 10.4. String and functions
- Lesson 11.1. Introduction to pointers in C
- Lesson 11.2. Syntax of C pointers
- Lesson 11.3. Dynamic memory allocation
- Lesson 11.4. Incrementing pointers
- Lesson 11.5. Pointers as function argument in C
- Lesson 12.1. Introduction to structures in C
- Lesson 12.2. Properties of structures in C
- Lesson 12.3. Structures and pointers
- Lesson 12.4. Structures and functions
- Lesson 13.1. Recursive functions in C
- Lesson 13.2. Depth of recursive functions
- Lesson 13.2. Mutual recursion
- Lesson 14.1. Additional exercises

Last update : 11/21/2022