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?
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?
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.
It is a left-shift operator.
It applies on the bit representation of 1 and shift it's bits to left by 2 position.
<<
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
.