Left shift and right shift calculation ..

12.6k Views Asked by At

i need to calculate left and right shift in java. in java i am able to code this. but i like to know how it calculate . Can any body here enplane me with Example. I am using below logic, plz check it's correct or not.

4>>2 ans is 1

Logic 4>>2 0100>>2 0001 = 1

1

There are 1 best solutions below

1
On

If I correctly understand what you're doing, $$n>>m=\frac{n}{2^m}$$ and $$n<<m=n\cdot 2^m.$$ This is similar to the way that moving a decimal point one place to the left (right) corresponds to division (multiplication) by $10$.

For example, then: $$16>>1=8\\16>>2=4\\16>>3=2,$$ and so on; or the other way, $$5<<1=10\\5<<2=20\\5<<3=40,$$ and so on.