Saturday, January 4, 2014

Right and Left Shift


Right Shift Operator (>>)
The Right shift Operator is represented by (>>) [pointing to direction it shifts the bits]
It requires to operands(on which operator operated). It shifts the bits of operand left to it towards right by places which is specified by the right of the operator.
The blanks created due to shifting are filled by 0s.

Operand to left of (>>) whose bits are shifted >> operand to right of (>>) number of places by which bits are shifted

1010>>3 equals 0001

Significance of Right shift

Lets take 25
Its binary equivalent
25 =  0001 1001
applying right shift
0001 1001>>1 = 0000 1100 = 12
applying a right shift to 12
0000 1100 >>1 = 0000 0110 = 6
applying a right shift to 6
0000 0110 >>1 = 0000 0011 = 3

At each step of right shift, we are actually dividing the given number by 2 (rejecting the decimal part if there is any 25/2 =12.5->we reject 0.5)

Left Shift Operator (<<)
The Left shift Operator is represented by (<<) [pointing to direction it shifts the bits]
It requires to operands(on which operator operated). It shifts the bits of operand left to it towards left by places which is specified by the right of the operator.
For each bit shifted, 0 is added to the right of the number.

0000 1110 << 1 =  0001 1100

Significance of Left Shift

Lets take 3
3 = 0000 0011
applying left shift to 3
0000 0011<<1 = 0000 0110 = 6
applying left shift to 6
0000 0011<<1 = 0000 1100 =12
applying left shift to 12
0000 1100<<1 = 0001 1000 =24

At each step of left shift, we are multiplying the number by 2.










No comments:

Post a Comment