-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
  • 5,791
  • 5
  • 29
  • 34
Abin Abraham
  • 91
  • 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,521
  • 10
  • 50
  • 75
0

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

WedaPashi
  • 3,350
  • 23
  • 40
Shay Gold
  • 405
  • 4
  • 13
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,350
  • 23
  • 40
Sourav Kanta
  • 2,697
  • 1
  • 16
  • 29