|
||||||
| Java Help Java Help forum discussing all Java platforms - J2ME, J2SE and J2EE - as well as relevant standards, APIs and frameworks such as Swing, Servlets, JSPs, Applets, Struts, Spring, Hibernate, ANT, EJB, and other Java-related topics. |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Display Modes |
|
|||
|
hi,
what does the mean of << operator in java example: Code:
static boolean won[] = new boolean[1 << 9];
static final int DONE = (1 << 9) - 1;
static void isWon(int pos) {
for (int i = 0 ; i < DONE ; i++) {
if ((i & pos) == pos) {
won[i] = true;
}
}
}
static {
isWon((1 << 0) | (1 << 1) | (1 << 2));
isWon((1 << 3) | (1 << 4) | (1 << 5));
isWon((1 << 6) | (1 << 7) | (1 << 8));
isWon((1 << 0) | (1 << 3) | (1 << 6));
isWon((1 << 1) | (1 << 4) | (1 << 7));
isWon((1 << 2) | (1 << 5) | (1 << 8));
isWon((1 << 0) | (1 << 4) | (1 << 8));
isWon((1 << 2) | (1 << 4) | (1 << 6));
}
Last edited by v0id; 07-18-2007 at 10:58 AM. Reason: Added code-tags. |
| Sponsored Links |
|
|
|
|||||
|
It's a bitwise-operator, used to manipulate bits.
"<<" is used to move each of the bits one place to the left. Code:
<< 01001011 = 10010110
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum C/C++ resources - C/C++ frequently asked questions Python resources - Python frequently asked questions I'm always up for a chat, so feel free to contact me... |
|
|||||
|
Code:
static boolean won[] = new boolean[1 << 9]; That's because: Code:
<< 0000000000000001 = 0000001000000000 Code:
static final int DONE = (1 << 9) - 1; Look above. Afterwards, you simply subtracts one from the result. Code:
if ((i & pos) == pos) {
That's because: Code:
1011 [11] & 1100 [12] = 1000 [08] Code:
... isWon((1 << 3) | (1 << 4) | (1 << 5)); ... That's because: Code:
<< 00000001 = 00001000 << 00000001 = 00010000 << 00000001 = 00100000 ------------ 00001000 | 00010000 | 00100000 = 00111000 It looks like the coder only made the code in that way, to confuse the reader. It's a stupid way to code, and it's only annoying to look at. Sometimes it's good to do bit manipulation, but I see no reasons to do it in your example.
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum C/C++ resources - C/C++ frequently asked questions Python resources - Python frequently asked questions I'm always up for a chat, so feel free to contact me... |
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| state operator worlds in LISP | weaverk | General Programming | 3 | 12-24-2006 10:41 AM |
| Java:Reference - Operators | John | Java Tutorials | 0 | 12-09-2006 10:05 AM |
| A custom String Cl*** | hoser2001 | C and C++ | 15 | 10-10-2006 06:05 PM |
| Xav | ........ | 1322.18 |
| MeTh0Dz|Reb0rn | ........ | 1053.7 |
| morefood2001 | ........ | 879.43 |
| John | ........ | 877.37 |
| marwex89 | ........ | 869.98 |
| WingedPanther | ........ | 830.24 |
| Brandon W | ........ | 735.07 |
| chili5 | ........ | 309.39 |
| Steve.L | ........ | 239.84 |
| dcs | ........ | 216.02 |
Goal: 100,000 Posts
Complete: 82%