Lost Password?


Go Back   CodeCall Programming Forum > Software Development > Java Help

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.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-17-2007, 08:46 AM
stack stack is offline
Learning Programmer
 
Join Date: Mar 2007
Posts: 39
Rep Power: 7
stack is on a distinguished road
Question << operator??

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 11:58 AM. Reason: Added code-tags.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 07-17-2007, 09:46 AM
v0id's Avatar   
v0id v0id is offline
Retired
 
Join Date: Apr 2007
Location: Denmark
Posts: 2,654
Last Blog:
CherryPy(thon)
Rep Power: 29
v0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of light
Send a message via MSN to v0id
Default

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
__________________
05-03-2007 - 11-13-2008
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 07-18-2007, 11:53 AM
stack stack is offline
Learning Programmer
 
Join Date: Mar 2007
Posts: 39
Rep Power: 7
stack is on a distinguished road
Default

i dont understand the meaning of <<operator and | operator
please explain this code???
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 07-18-2007, 12:23 PM
v0id's Avatar   
v0id v0id is offline
Retired
 
Join Date: Apr 2007
Location: Denmark
Posts: 2,654
Last Blog:
CherryPy(thon)
Rep Power: 29
v0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of light
Send a message via MSN to v0id
Default

Code:
static boolean won[] = new boolean[1 << 9];
It makes an array of 512 booleans.
That's because:
Code:
<< 0000000000000001
 = 0000001000000000
And 0000001000000000 is binaries equivelant of decimals 512.

Code:
static final int DONE = (1 << 9) - 1;
It initializes DONE with a value of 511.
Look above. Afterwards, you simply subtracts one from the result.

Code:
if ((i & pos) == pos) {
Let's suppose i is 11 and pos is 12. Then the first calculation will give 8, and that's not equivelant to pos.
That's because:
Code:
  1011 [11]
& 1100 [12]
= 1000 [08]
Code:
...
isWon((1 << 3) | (1 << 4) | (1 << 5));
...
This will pass 56 to the function, isWon.
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.
__________________
05-03-2007 - 11-13-2008
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 07-18-2007, 07:37 PM
evanator2005 evanator2005 is offline
Newbie
 
Join Date: Jul 2007
Posts: 2
Rep Power: 0
evanator2005 is on a distinguished road
Default

aaaaaaaaaahhhhhhhhhhhhhhh!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #6 (permalink)  
Old 07-18-2007, 08:21 PM
Frantic's Avatar   
Frantic Frantic is offline
Learning Programmer
 
Join Date: May 2006
Posts: 92
Rep Power: 10
Frantic is on a distinguished road
Default

Like everyone else said, shift the bits left.

Quote:
Originally Posted by evanator2005 View Post
aaaaaaaaaahhhhhhhhhhhhhhh!
What does this mean?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
state operator worlds in LISP weaverk General Programming 3 12-24-2006 11:41 AM
Java:Reference - Operators John Java Tutorials 0 12-09-2006 11:05 AM
A custom String Class hoser2001 C and C++ 15 10-10-2006 07:05 PM


All times are GMT -5. The time now is 12:09 AM.

Contest Stats

WingedPanther ........ 2753.6
Xav ........ 2704
Brandon W ........ 1702.32
John ........ 1207.73
marwex89 ........ 1175.24
morefood2001 ........ 966.05
dcs ........ 655.75
Steve.L ........ 475.59
orjan ........ 418.58
Aereshaa ........ 383.54

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 100%


Complete - Celebrate!

Ads