• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

operator

 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi there,
int i=10,j=3,k=7
int p=30
what is the vaule of i,j,k,p after the execution of the following
line:
p+=k-=j<<=i%=4
i know first: p+=(k-=(j<<=(i%=4)))
second : p+=(k-=(j<<=(i=2)))
third; p+=(k-=(j<<=2))
fourth p+=(k-=(j=0))
........
when arrived fourth, i don't understand why j=12(the answer provided)?
could someone explain to me?
thanks in adv...
 
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by fengqiao cao:
hi there,
int i=10,j=3,k=7
int p=30
what is the vaule of i,j,k,p after the execution of the following
line:
p+=k-=j<<=i%=4
i know first: p+=(k-=(j<<=(i%=4)))
second : p+=(k-=(j<<=(i=2)))
third; p+=(k-=(j<<=2))
fourth p+=(k-=(j=0))
........
when arrived fourth, i don't understand why j=12(the answer provided)?
could someone explain to me?
thanks in adv...


Hi associativity ops (op=) work from Right to Left so there are a couple of steps in the equation:
1. 10 % 4 = 2
2. 3 <<= 2 = 0011 << evaluate and assign 2 places left = 1100 (in binary - i've left off the rest of the 0s) converting this back gives 12
I think in terms that in each position the bits are placeholders for decimals 8421 so for the 1100 bits there is 1 bit in position 8 and one bit in postion 4 this gives 12 (not really scientific but it works for me
all the best - Jim
 
fengqiao cao
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi jim
thans for your reply
i am sorry i didn't get it.
as far as i know j<<=i...is evalated as j=j<<2.
could you give me more detail!?
 
Jim Petersen
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi you've got it!

as far as i know j<<=i...is evalated as j=j<<2.


where j is 3 so... 3 << 2 - signed left shift 2 places is
0000 0000 0000 0000 0000 0000 0000 0011 // integer 3 in binary (all 32 bits for int shown here)

0000 0000 0000 0000 0000 0000 0000 1100 // produces 12 (int)
---------------------------------------
hope this helps - Jim

[This message has been edited by Jim Petersen (edited November 16, 2001).]
 
We cannot change unless we survive, but we will not survive unless we change. Evolving tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic