the << operator

azcalv408
azcalv408 used Ask the Experts™
on
Hi,
   What is the << operator?  such as
   x = (y << 1);
  Is there an equivalent way of expressing it? like in if/else/other operators statements?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Java Developer
Top Expert 2010
Commented:
>Is there an equivalent way of expressing it?
a left shift by 1(<<1) is equivalent to multiplication by 2.
a left shift by n is same as multiplication by 2^n(2 raised to the power n).

similarly a right shift by n(>>n) is same as division by 2^n.

so, x = (y<<1);
is same as x = y*2;
but the shift is a  faster operation than multiplication, and hence usually preferred (only useful when multiplying by powers of 2 though).

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial