What does this do?
Author |
Message |
SJ
|
Posted: Sun Apr 13, 2008 3:44 pm Post subject: What does this do? |
|
|
What does ">>" and "<<" mean or do in Java? I've seen it in contest solutions on topcoder several times, but I have no idea what they do..
so like,
for (int i = 0; i < 1 << 3; i++)
System.out.println (i);
prints numbers from 1 to 7
for (int i = 0; i < 1 << 13; i++)
System.out.println (i);
prints from 1 to 8191
i mean, they even got arrays difined like int [] a = new int [1<<3]; and if statements like if (x < (2<<5))
what does this symbol mean?
and what do you call it? |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
Dan
![](http://wiki.compsci.ca/images/archive/3/3c/20100325043407!Danspic.gif)
|
Posted: Sun Apr 13, 2008 4:02 pm Post subject: RE:What does this do? |
|
|
<< is the left shift operator and >> is the right shift operator.
For more info see: http://java.sun.com/docs/books/tutorial/java/nutsandbolts/op3.html
It bascly shifts all the bits in the numer left or right by a given amount. <<< and >>> are unsinged versions of the same command.
You probly don't want to use thess comands on decmal numbers unless you know what you are doing, understand binnary and how numbers are repserented in bits. |
Computer Science Canada
Help with programming in C, C++, Java, PHP, Ruby, Turing, VB and more! |
|
|
|
![](images/spacer.gif) |
SJ
|
Posted: Sun Apr 13, 2008 6:29 pm Post subject: RE:What does this do? |
|
|
Wow I never knew these existed. Thanks dan, that link felt like a holy grail if anything ![Smile Smile](http://compsci.ca/v3/images/smiles/icon_smile.gif) |
|
|
|
|
![](images/spacer.gif) |
|
|