This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!See this thread for details.
Big Moose Saloon
 Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies Register / Login Win a copy of Refactoring for Software Design Smells: Managing Technical Debt this week in the OO, Patterns, UML and Refactoring forum!

# Q on complex assignment operators

Ranch Hand

Joined: Oct 29, 2004
Posts: 52
This question is from whizlabs practice exam.

Check this out:
int i=10, j=3, k=7;
int p=30;
p += k -= j <<= i %= 4
What are the values of i,j,k,p?

This is how I interpret it: From right to left
p += (k -= (j <<= (i %= 4)))
Is this right?
1) (i %= 4) evaluates to 2 so i=2
2) (j <<= (i %= 4)) how do u evaluate j?? What does the <<= operator do?
Thanks
Nitin Bhagwat
Ranch Hand

Joined: Sep 09, 2004
Posts: 132
i %= 4 ==> 2
j<<= 2 ==> 12
k-=j ==> -5
p+=k ==> 25

"Imagination is more important than knowledge. Knowledge is limited. Imagination encircles the world."
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
p += (k -= (j <<= (i %= 4)))
Is this right?

yes
1) (i %= 4) evaluates to 2 so i=2

yes
2) (j <<= (i %= 4)) how do u evaluate j?? What does the <<= operator do?

shift the bits in j to the left i%4 places

Mike Gershman
SCJP 1.4, SCWCD in process

Have you checked out Aspose?

subject: Q on complex assignment operators