# Left Shifts

The left-shift operator causes the bits in shift-expression to be shifted to the left by the number of positions specified by additive-expression. The bit positions that have been vacated by the shift operation are zero-filled

## We'll cover the following

## Left shift

The left shift operator is written as `<<`

.

Integers are stored in memory as a series of bits.
For example, the number `6`

stored as a 32-bit `int`

would be:

```
6 = 00000000 00000000 00000000 00000110
```

Shifting this bit pattern to the left one position `(6 << 1)`

would result in the number `12`

:

```
6 << 1 = 00000000 00000000 00000000 00001100
```

As you can see, the digits have shifted to the left by one position, and the last digit on the right is filled with a zero. You might also note that shifting left is equivalent to multiplication by powers of 2.

So,

```
6 << 1 → 6 * 2^1 → 6 * 2
6 << 3 → 6 * 2^3 → 6 * 8
```

A good optimization compiler will replace multiplications with shifts when possible.

```
32-bit representation
00000000 00000000 00000000 00000010 << 1 → 00000000 00000000 00000000 00000100
4-bit representation
0010 << 2 → 1000
```

A single left shift multiplies a binary number by 2, as seen above.

```
0010 << 1 → 0100
0010 is 2
0100 is 4
```

So, in simple terms, `<<`

(left shift) takes two numbers, left shifts the bits of the first operand, and the second operand decides the number of places to shift.

Let’s see how to represent this in a mathematical formula.

## Formula

a << b = ( a * $2^{b}$ )

## Code

Level up your interview prep. Join Educative to access 70+ hands-on prep courses.