-3

I am working on a simple project in C where I encountered this statement in one of the tutorials.

int i = 1 << 2 ;   

What does this statement actually do?

Biffen
  • 6,249
  • 6
  • 28
  • 36
Abin Abraham
  • 101
  • 1
  • 1
  • 6

4 Answers4

2

This is also called the shift operator. It will shift de bits n positions to the left. Shifting the bits by one positions is the same as multiplying or dividing by 2.

2

It is a left-shift operator.

It applies on the bit representation of 1 and shift it's bits to left by 2 position.

Razib
  • 10,965
  • 11
  • 53
  • 80
0

Shift left is a bitwise operation. Look here: Bitwise operations in C wiki

WedaPashi
  • 3,561
  • 26
  • 42
Shay Gold
  • 403
  • 4
  • 14
0

<< is a binary Left Shift operator. The left operands value is shifted left by the number of bits specified by the right operand. and 0 are padded in the right. So the binary of 1 i.e, 1 is shifted left 2 places and becomes 100 which is 4 in decimal number system. So i is assigned 4.

Here is a quick tutorial that explains bitwise operations

WedaPashi
  • 3,561
  • 26
  • 42
Sourav Kanta
  • 2,727
  • 1
  • 18
  • 29