Search Discussion-Lab

Wednesday, September 28, 2011

Easy concept: Bitwise right and left shift << / >>

">>" is called the Bitwise right shift operator and "<<" is called the Bitwise left shift. Why bitwise? Cause this operator is used as a easy tool for programmers working with bytes. If you just think about the results of two numbers compared with this operator you will be fixed. Cause, this operator changes bit orders in a number. If you don't have the knowledge of bytes or bits simply read this lesson. Now for the clear concept let's see the actual calculation by this operator in decimal.


A << B = A * (2^B)


Here "^" is the "to the power", not any other operator. Now see some example:


2 << 3 = 16 1 << 2 = 4 20 >> 2 = 5
120 >> 3 = 15


Now you are fixed, as I told you. Let's see the same examples in binary expression:


10 << 3 = 10000 1 << 2 = 100 10100 >> 2 = 101
1111000 >> 3 = 1111
A << B = C


What happened on above examples? The left shift (<<) adds B number of zeros to the end of A. And the right shift cut's off B number of zeros from the end of A. This is all about Left/Right shift.