Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Q on complex assignment operators

 
Sumithab Baskaran
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i %= 4 ==> 2
j<<= 2 ==> 12
k-=j ==> -5
p+=k ==> 25
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic